1.gameobject.find ():
Look for the game object in the hierarchy panel where the activie is not false;
Paths as official examples:
public class Exampleclass:monobehaviour {
Public Gameobject hand;
void Example () {
Hand = Gameobject.find ("Hand");
Hand = Gameobject.find ("/hand");
Hand = Gameobject.find ("/monster/arm/hand");
Hand = Gameobject.find ("Monster/arm/hand");
}
}
notes:
Gameobject.find () is convenient to use if your gameobject is born acive = False. Then you use Gameobject.find () is never able to get its object. If the object is not obtained, then the object on the body of the script AH component ah what is not acquired, become meaningless objects.
The Gameobject.find () method is used very often in games. But it also consumes performance, you can think of its principle is certainly also in a similar recursive form to do, then we should try to call the Gameobject.find () method Less, can get the game object, saved in memory, this is the best choice. Especially in the Update method do not go to the Find () Game Object!!
2.transform.find ()
The official explanation is to obtain a sub-object by name (Finds a children by name and returns it), the method can get to the hidden (inactive) Gameobject,
Can be obtained by first obtaining the parent object (active must be true), and then by looking for the child Transform.find ()
1 gameobject root = Gameobject.find ( " gameobject " 2 3 gameobject xxxx = Root.transform.Find ( " xxxx " ). Gameobject; 4 5 xxxx. SetActive (true );
notes:
Remember the above I said with Gameobject can't get natural acive = False Game object, if you use Transform.find () can be very good to get, in addition Unity also provides a transform.findchind () method , this method will be abandoned by unity in the future, it is best not to use, with Transform.find () can be replaced.
In the following code, we first get the top-level object root. Then use Find () to find its child node "XXXX" object, regardless of whether the "XXXX" object is active = True can be directly found object.
The Find () method can only go directly to the child nodes, if you want to find the Sun node, then you can use the "/" symbol to separate the hierarchical relationship, it is convenient to find. Similarly, whether the "XXXX" object is active = True, the object can be found directly.
Gameobject cube = root.transform.Find ("xxxx/cube"). Gameobject;
It is also important to note that unity specifies that the parent node active = True and that the child node's active = True is sufficient to be fully displayed. The use of Transform.find () can be very convenient to get the game object, because of the game object, then its body of the script AH components ah what can be easily obtained.
But Transform.find () must ensure that the activity of your top-level Parent Object = True.
For example, you made a scene with some maps that you pre-Activie = False in the scene, and you want to turn them all on at some point in the game setActive (true)
You can put the "map" node on an active = True gameobject, which is convenient to write in either the off or display code. If your map node is a top-level node, once it is inherently acive = False, then you will not be able to get its object, and you cannot set its properties.
1 gameobject root = Gameobject.find ("Gameobject"); 2 3 gameobject map = root.transform.Find ("map"). Gameobject; 4 5 map. SetActive (true);
3. Unity also provides several ways to get a game object
Gameobject.findgameobjectswithtag ("tag") Gameobject.findwithtag ("tag")
Based on a tag to get the game object, return one or an array, I personally think that this two method is not useful, because since the need to use the tag so compared to this game object is necessarily a very special one, so I will put it in memory.
Object.findobjectoftype
Object.findobjectsoftype
Resources.findobjectsoftypeall
Returns an object based on a type, such as Gameobject, Texture, Animation, or even a generic script that you write yourself. It is convenient to find and can return one or an array. I think these methods are not really useful in the game, but it is very often used in the editor, such as you want to do a batch inspection of the scene of the tool, to find out if there is a particular type of object in the scene. Or look at the memory footprint to see if the current in-memory texture are not released. Wait a minute.
There is also a method, if you know the index from the object, you can also use the following method to get, the parameter is index.
transform.GetChild(index);index为索引,0,1,2,3,4代表第几个child.
Unity3d_01_ various ways to find Gameobject