- Home /
Is there any analog of GameObject.Find() function?
I don't understand your question. Please explain further.
for example: I want to change position of an object and to do this I have to write "GameObject.Find("objectName").transform.position = new Vector3 (3,0,0)".. is there any other way to refer to some specific object without using the Find() function?
Answer by robertbu · Apr 18, 2014 at 03:31 PM
There are a couple of other ways, but all are similar. For example you ca use a tag and use GameObject.FindWithTag(). But typically what you want to do is to cache the Find() for future use:
private var player : Transform;
function Start() {
player = GameObject.Find("Player").transform;
}
Then anytime in the rest of the code you want to set the position, you would do:
player.position = Vector3(3,0,0);
You can also use drag and drop. That is if you have a public variable:
var player : Transform;
or in C#:
public Transform player;
Then you can drag and drop the player game object on the 'player' variable in the Inspector to form a link.
GameObject.Find() is not very efficient, so you want to avoid calling it every frame.
Answer by Andrew_Kenady · Apr 18, 2014 at 03:32 PM
There are many ways to locate and manipulate game objects.
FindGameObjectWithTag(string tag)
and many more!
You can also declare a public GameObject at the top of your script, and then assign it in the Editor with drag-and-drop.
Your answer
Follow this Question
Related Questions
Creating a teleportation gun 1 Answer
How to make a GameObject follow my Player 2 Answers
Destroying an object after its out of sight ! 2 Answers
What is wrong with this script? 2 Answers
create a destroyer for 2d infinite runner game objects. 2 Answers