Interaction between the different Unity basis --- GameObject

By script variables

The most direct way to find GameObject is to add a public GameObject variables in the script:

public class SphereScript : MonoBehaviour
{
    public CubeScript cube;
   //其他代码
}

This variable will be like any other variable in the Inspector visible:
Here Insert Picture Description
you can now assign a scene from the panel or drag the object hierarchy on this variable. GetComponent function andComponent (Component) Access is available as a variable to this object and other objects, you can use the following code:

void Update()
    {
        cube.CubeFunction();
    }

In addition, if you declare a public variable component type in a script, you can drag any GameObject with the component. This will directly access the components rather than GameObject itself.

public Transform playerTransform;

When processing a single object having a permanent connection, using variable objects linked together is the most useful. You can use an array variable to link several objects of the same type, but the connection must still be in the Unity editor, rather than at run time. Positioning objects at run time is often convenient, Unity provides two basic methods, as described below.

GameObject find the child objects (Child)

Sometimes, the game scene will use many of the same type of game objects, such as enemies, road signs and obstacles. These may need to be tracked by a particular script that they monitor or react (e.g., all signs point may need to script pathfinding available). Use these variables to link GameObjects is possible, but if each new waypoint must be dragged on a variable in the script, then the design process becomes tedious. Similarly, if a waypoint is deleted, you must delete references to variables missing GameObject very troublesome. In this case, by a group of sub-objects of a set GameObject GameObject to manage them usually better. Sub gameobject can be retrieved by Transform Component parent object (because all gameobject have implied a Transform):

using UnityEngine;
public class WaypointManager : MonoBehaviour {
    public Transform[] waypoints;
    
    void Start() 
    {
        waypoints = new Transform[transform.childCount];
        int i = 0;
        
        foreach (Transform t in transform)
        {
            waypoints[i++] = t;
        }
    }
}

You can also navigate to the child objects of a particular name by Transform.Find () method:

transform.Find("Gun");

When an object has a child object can be added and deleted in the course of the game, which will be very useful. Lay down a weapon is a good example of this can be picked up.

(Label) to find the object game by Name (name) or Tag

As long as you have some information to identify the object game, you can always find a game object anywhere in the hierarchy of the scene. Individual objects can be retrieved by GameObject.Find () Method:

GameObject player;

void Start() 
{
    player = GameObject.Find("MainHeroCharacter");
}

An object or collection of objects may be used to retrieve GameObject.FindWithTag and GameObject.FindGameObjectsWithTag Tags:

GameObject player;
GameObject[] enemies;

void Start() 
{
    player = GameObject.FindWithTag("Player");
    enemies = GameObject.FindGameObjectsWithTag("Enemy");
}

Guess you like

Origin blog.csdn.net/u014128662/article/details/90765883