Unity how to reference an object from a different one

Adarkuccio

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?

Roberto

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;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to reference an object from a different class [C#]

From Dev

How to pass the reference from inside one object to the constructor of another

From Dev

Trying to create new reference from one ArrayList to a different ArrayList is moving, not copying, my original object

From Dev

How to pass the User object from one Controller to the different JavaFX Controller?

From Dev

How variable different from the reference?

From Dev

Moving object from one postion to another in unity

From Dev

Unity - How do I apply an animation from one game object to another rigged game object?

From Dev

How do I reference different model types from one field in Django?

From Dev

How do I reference different model types from one field in Django?

From Dev

Object reference not set to an instance of an object on querying from different lists

From Dev

How to convert an array of objects (of different lengths) into one object that has different properties from the elements in the original array?

From Dev

Unity - passing script by reference from editor, instantiating it on object at runtime

From Dev

Undefined Reference when Invoking Function of One Object from Another Object

From Dev

Different object and reference

From Dev

How to make custom inspector add object reference in Unity

From Dev

How to enforce one-to-one object reference when instantiating?

From Dev

How to enforce one-to-one object reference when instantiating?

From Dev

Use a reference from one workbook in a formula of a different workbook

From Dev

How to reference a 'var' from one method in another?

From Dev

How to determine if an object is the same one or different

From Dev

Pass object by reference to a different object

From Dev

how to reference a variable from the parent object in javascript

From Dev

how to reference a method from within an object

From Dev

How to reference a field from within an object

From Dev

How to fetch reference string from CKReference object

From Dev

Unity C# - for loop: go from one object to the next one (movement) - gets stuck on first object

From Dev

Reference same object with different tags

From Dev

How to tidy data from different columns into one

From Dev

how to create one patch from different commits?

Related Related

  1. 1

    How to reference an object from a different class [C#]

  2. 2

    How to pass the reference from inside one object to the constructor of another

  3. 3

    Trying to create new reference from one ArrayList to a different ArrayList is moving, not copying, my original object

  4. 4

    How to pass the User object from one Controller to the different JavaFX Controller?

  5. 5

    How variable different from the reference?

  6. 6

    Moving object from one postion to another in unity

  7. 7

    Unity - How do I apply an animation from one game object to another rigged game object?

  8. 8

    How do I reference different model types from one field in Django?

  9. 9

    How do I reference different model types from one field in Django?

  10. 10

    Object reference not set to an instance of an object on querying from different lists

  11. 11

    How to convert an array of objects (of different lengths) into one object that has different properties from the elements in the original array?

  12. 12

    Unity - passing script by reference from editor, instantiating it on object at runtime

  13. 13

    Undefined Reference when Invoking Function of One Object from Another Object

  14. 14

    Different object and reference

  15. 15

    How to make custom inspector add object reference in Unity

  16. 16

    How to enforce one-to-one object reference when instantiating?

  17. 17

    How to enforce one-to-one object reference when instantiating?

  18. 18

    Use a reference from one workbook in a formula of a different workbook

  19. 19

    How to reference a 'var' from one method in another?

  20. 20

    How to determine if an object is the same one or different

  21. 21

    Pass object by reference to a different object

  22. 22

    how to reference a variable from the parent object in javascript

  23. 23

    how to reference a method from within an object

  24. 24

    How to reference a field from within an object

  25. 25

    How to fetch reference string from CKReference object

  26. 26

    Unity C# - for loop: go from one object to the next one (movement) - gets stuck on first object

  27. 27

    Reference same object with different tags

  28. 28

    How to tidy data from different columns into one

  29. 29

    how to create one patch from different commits?

HotTag

Archive