Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Yet another time, how do I access right components in other gameobjects?

Discussion in 'Scripting' started by MxM111, Oct 3, 2014.

  1. MxM111

    MxM111

    Joined:
    Sep 27, 2014
    Posts:
    7
    Here is some version of the code I have. I have simplified it, so some typos are possible.
    Fist object has this script:
    Code (csharp):
    1.  
    2. public class GeneratePins : MonoBehaviour
    3. {
    4.         public GameObject prefab;
    5.         private GameObject myObject;
    6.  
    7.         void Start ()
    8.         {
    9.           myObject= (GameObject) Instantiate (prefab, ...other_pars...);
    10.           PinHandler handler = myObject.GetComponent<PinHandler> ();
    11.           handler.SetVector (new Vector3 (2f, 2f, 2f));
    12.         }    
    13. }
    14.  
    For prefab variable I set a prefab in Unity (drug into the prefab filed in of the first object) that has the following script (again, simplified), which is class PinHandler.

    Code (csharp):
    1.  
    2. public class PinHandler : MonoBehaviour
    3. {
    4.         public Vector3 Target;
    5.  
    6.         void Start ()
    7.         {
    8.           Target = new Vector3(0,0,0);
    9.         }
    10.  
    11.         public void SetVector (Vector3 _Target)
    12.         {
    13.                 Target = _Target;      
    14.                 print ("TargetSet");
    15.                 print (Target);
    16.         }
    17.  
    18.         void Update ()
    19.         {
    20.                 print (Target);
    21.         }
    22. }
    23.  
    So, what do you think I am getting as print out? First I do get "TargetSet" and correct Target, which is 2, 2, 2, as expected. But then, when updates come, they print 0,0,0, which is result of initialization. Basically I am changing Target of some other temporary GameObject (or what else?), despite of what I wrote. HELP! Is something wrong with typcasting of my Instantiate line?
     
  2. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    PinHandlers Start() get's called after SetTarget

    Try:

    Code (CSharp):
    1. public Vector3 Target = Vector3.zero;
    2.         void Start ()
    3.         {
    4.             Debug.Log("Start!");
    5.         }
    6.         public void SetVector (Vector3 _Target)
    7.         {
    8.             Debug.Log("Target vector set!");
    9.                 Target = _Target;    
    10.                 print ("TargetSet");
    11.                 print (Target);
    12.         }
    13.         void Update ()
    14.         {
    15.                 print (Target);
    16.         }
     
    MxM111 likes this.
  3. MxM111

    MxM111

    Joined:
    Sep 27, 2014
    Posts:
    7
    That would make sense, though I can't check it right away (not at computer with Unity). It is possible that Start() is handled by game engine out of order of execution, not when instantiate is called. However, I do need to initialize PinHandler class (as I said, the code is simplified, I want to do much more in Start, before I call other functions of PinHandler). I can't write constructor, since it has to be instantiated by Unity. I guess I can have InitObject() method of PinHandler that I have to call before first time I use any other function of PinHandler, but it is ugly, since there is probability that I will forget calling it. Bleh. Is there some method I can define which is guaranteed to be called on instantiate? OnIntantiate() kind of thing?
     
  4. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    Awake() maybe
     
    MxM111 likes this.
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Move the pin handlers Start() stuff to awake
     
    MxM111 likes this.
  6. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    Well there are couple of ways to deal with this.

    1. Add delegate/call-back that executes a method after start on the GeneratePins class and takes in the Pinhandler as parameter.
    2. Add reference of GeneratePins to your pinhandler and make some sort of onready method to your GeneratePins class that takes in Pinhandler as parameter and modifies it based on your needs. then use that after start.
     
    MxM111 likes this.
  7. MxM111

    MxM111

    Joined:
    Sep 27, 2014
    Posts:
    7
    Thank you everyone. Just wanted to report here that everything you said is confirmed and Awake() is working as you have suggested.