Search Unity

How do I access an Instantiated object?

Discussion in 'Scripting' started by ExpoExo, Jan 3, 2017.

  1. ExpoExo

    ExpoExo

    Joined:
    Jan 3, 2017
    Posts:
    3
    So, pretty far into the game I stumble onto a problem.. I can not access an instantiated object. I do not know how, because I am relatively new into this language. I have tried using for example "name = Instantiate" but I still can not access the rigid body of the instantiated object. Also I need help in connecting a scrip to an instantiated object. For example i have one instantiated object with a script attached to it which is giving the player the ability to move the object with the mouse, but as I click on the Instantiated object it comes out as an error = "Object reference not set to an instance of an object". Could it be because it is a copy of the original?
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    You can access the gameobject with this (in C#):
    GameObject instantiated = (GameObject)GameObject.Instantiate...
    you can get the rigidbody with instantiated.GetComponent<Rigidbody>()
    connecting a script? you mean adding a component:
    instantiated.AddComponent<yourtypehere>()
    And the last one is something you haven't done, there's nothing that changes when something is a clone
    You've probably done some initialization in the Start() and that wont run
     
    ExpoExo likes this.
unityunity