Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity how to reference an object from a different one

Tags:

c#

unity3d

I'm learning Unity and for what I know you can add components to an object, so let's say I do have an object with a component "Move" where you use the Unity API to do some operations.

Well the thing I'm not yet understanding is, since Unity works with components (it is a sort of Component Based Architecture if I'm not wrong) and the architecture of the code is really different from what I'm used to work with... How can I reference an object from a different one?

take as an example Unity 4.3 in 2D, if I have a sprite object and I want another sprite object to follow the first one, he must know the position of the first one... well, how can I reference the first sprite object, to the second one ? I know one way is to use TAGs but I don't think it is a good idea to always use TAG, if you need to reference a lot of object you surely need a different way

an add to the question: this question is about how to references object that actually exist (added via the IDE) but what if I create an object programmatically and then I need to reference it?

like image 554
Adarkuccio Avatar asked Mar 20 '23 06:03

Adarkuccio


1 Answers

There are many ways to do it, it all depends on the situation and how you organise your project.

You can find game objects by name using GameObject.Find(string), you can use UnityEngine.Object.FindObjectsOfType(type) to find objects that contain a certain component (like your example). Note that it is not recommended to call most (all?) find methods in the Update() method, for performance reasons.

Depending on how you architect your scene hierarchy, you can easily get objects that are related: game objects in the same hierarchy tree are connected via the Transform API: Transform.parent will return the transform of the parent game object and Transform.GetChild(int) returns the transform of the children defined by the index, and Transform.Find(string) returns a child transform that matches the name (not recursive).

Why are they connected via the Transform API? Because transform is the physical representation of the object, its position, scale, and rotation. Objects that are under other objects will suffer the same changes of their parent. You mentioned one object "following" the other, so if you meant physically following, this can simply be achieved by putting the follower under the followed in the hierarchy.

There's also the editor way: if you create a public reference to an object in your script, Unity will create an entry for it in the editor. Simply attach your script with the public reference to a Game Object in your scene and you'll see the reference in the Inspector window. Then you can drag any other object that matches the type of your public reference, to this reference in the Inspector and you can use it in your script.

Finally, you can do it programmatically: create a static class that contains a pool of your game objects, or use the singleton pattern, whatever you'd do in normal code.

Also, UnityEngine.Object.Instantiate() will return the reference to the instantiated object so you can use it. Next, you can also use gameObject.GetComponent<YourScript>() if you just instantiated a GameObject to access your script; something like this:

public class MyScript : MonoBehaviour {
    public void GameObject creator;
    public void CreateClone() {
        GameObject clone = Instantiate(this);
        MyScript myScript = clone.GetComponent<MyScript>();
        myScript.creator = this;
    }
}
like image 168
Roberto Avatar answered Apr 01 '23 20:04

Roberto