using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
public Lesson3 otherLesson3;
// Start is called before the first frame update
void Start()
{
#region Knowledge Point One Important Members
//1. Get the attached GameObject
print(this.gameObject.name);
//2. Get the position information of the attached GameObject
// Get object position information
print(this.transform.position);// Position
print(this.transform.eulerAngles);// Angle
print(this.transform.lossyScale);// Scale size
// This writing method has the same effect as above, both get the position information of the attached object
//this.gameObject.transform
//3. Get whether the script is active
this.enabled = false;
// Get other script objects, attached gameobject and transform position information
print(otherLesson3.gameObject.name);
print(otherLesson3.transform.position);
#endregion
#region Knowledge Point Two Important Methods
// Get other scripts mounted on the attached object
//1. Get the single script mounted on itself
// Get by script name
// The method to get the script, if it fails to get, it means there is no corresponding script and will return null by default
Lesson3_Test t = this.GetComponent("Lesson3_Test") as Lesson3_Test;
print(t);
// Get by Type
t = this.GetComponent(typeof(Lesson3_Test)) as Lesson3_Test;
print(t);
// Get by generic, it is recommended to use generic because it does not require secondary conversion
t = this.GetComponent<Lesson3_Test>();
if( t != null )
{
print(t);
}
// As long as you can get other objects in the scene or the scripts attached to the objects
// then you can get all its information
//2. Get multiple scripts mounted on itself
Lesson3[] array = this.GetComponents<Lesson3>();
print(array.Length);
List<Lesson3> list = new List<Lesson3>();
this.GetComponents<Lesson3>(list);
print(list.Count);
//3. Get scripts mounted on child objects (it will also look for whether the script is mounted on itself by default)
// The function has a parameter, which defaults to false, meaning that if the child object is inactive, it will not look for whether there is a certain script on this object
// If true is passed, it will look even if inactive
// Get child object mounted script single
t = this.GetComponentInChildren<Lesson3_Test>(true);
print(t);
// Get child object mounted scripts multiple
Lesson3_Test[] lts = this.GetComponentsInChildren<Lesson3_Test>(true);
print(lts.Length);
List<Lesson3_Test> list2 = new List<Lesson3_Test>();
this.GetComponentsInChildren<Lesson3_Test>(true, list2);
print(list2.Count);
//4. Get scripts mounted on parent objects (it will also look for whether the script is mounted on itself by default)
t = this.GetComponentInParent<Lesson3_Test>();
print(t);
lts = this.GetComponentsInParent<Lesson3_Test>();
print(lts.Length);
// It also has a list, omitted, the same routine as above
//5. Try to get the script
Lesson3_Test l3t;
// Provides a safer method to get a single script. If obtained, it will return true
// Then you can proceed with logical processing
if(this.TryGetComponent<Lesson3_Test>(out l3t))
{
// Logical processing
}
#endregion
}
// Update is called once per frame
void Update()
{
}
}