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

Setting a gameobject in one script from another at runtime?

Discussion in 'Scripting' started by frankmat, Jul 11, 2014.

  1. frankmat

    frankmat

    Joined:
    Sep 14, 2013
    Posts:
    42
    I've given up trying to figure this out for myself after many hours and I assumed it should be simple.

    How do you assign a gameObject from 1 script to another? ScriptA which is my main gameEngine has 11 players inside an array. ScriptB does all sorts of player animation and IK.

    Obviously it works fine if I drag and drop a gameObject in the inspector but I am using UMA to dynamically build the 11 players so I need to do it in the code at runtime.

    What am I missing that should be simple?

    ScriptA
    Code (CSharp):
    1.  
    2. public GameObject[] players;
    3.  
    4. player[0] = ................................
    5. player[1] = ................................
    6. player[2] = ................................
    7.  

    ScriptB
    Code (CSharp):
    1.  
    2. public GameObject character;
    3.  
    4. void DomyStuffhere(character activeCharacter) {
    5. ...............................
    6. }
    7.  
     
  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    Are you talking about a assigning a prefab to it? Or just another object in the Hierarchy?

    If it's the second, you can access it as...

    Code (csharp):
    1. playersObject.getComponent<YourScript>().players[index] = gameObject;
    With playersObject being the object with the first script attached, YourScript is the name of the script attached to it, and index is an index in the array. This is where you'd access it from the second script.

    Erik
     
  3. frankmat

    frankmat

    Joined:
    Sep 14, 2013
    Posts:
    42
    Hmmm either I'm confused or we both are :) I'll try to explain a little better.

    I have 2 scripts assigned to empty gameObjects. 1 script is the Game Engine. the other assigns some IK to an avatar created using a UMA generated GO. UMA generates 11 characters for me in the gameEngine.cs file (code not shown). Once generated I then assign all of those created players gameObjects to an Array (shown below in gameEngine.cs).

    The UMAFBBIK.cs file does the IK... eg: It will have a certain player bend over and pick up a ball
    What I need to do is in my gameEngine.cs assign at runtime the gameObject called umaCharacter inside the UMAFBBIK.cs file from the gameEngine.cs script. I want to specify which player is going to use IK to pick up the ball.

    gameEngine.cs
    Code (CSharp):
    1.  
    2. public GameObject[] players;
    3.  
    4. void Start ()
    5. {
    6.    //Find all the GO's whcih have a tag of 'Player' and assign to an array
    7.    players = GameObject.FindGameObjectsWithTag("Player").OrderBy(player => player.name).ToArray();
    8. }
    9.  
    10. void Update () {
    11.  
    12.   //Have player pick up the ball
    13. //What is the script in here that I need to assign the gameObject???
    14.  
    15. }
    16.  
    17.  

    UMAFBBIK.cs
    Code (CSharp):
    1.  
    2. public GameObject umaCharacter;
    3. public BipedReferences references;
    4. public Vector3 leftHandOffset;
    5. public FullBodyBipedIK ik;
    6.  
    7. void Update () {
    8.  
    9. if (umaCharacter != null){
    10.  
    11. BipedReferences.AutoDetectReferences(ref references, umaCharacter.transform,  BipedReferences.AutoDetectParams.Default);
    12.  
    13. ik = umaCharacter.AddComponent<FullBodyBipedIK>();
    14. ik.SetReferences(references, null);
    15. ik.solver.SetLimbOrientations(BipedLimbOrientations.UMA);
    16.  
    17. umaCharacter = null;
    18.  
    19.   }
    20. }
    21.  
    22. void LateUpdate() {
    23.    if (ik != null) {
    24.       ik.solver.leftHandEffector.positionOffset += ik.solver.GetRoot().rotation * leftHandOffset;
    25.    }
    26. }
    27.  
    28.  
     
    Last edited: Jul 11, 2014
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    give gameengine a reference to the umafbbik script and drop it into the slot in the inspector, you can then either directly do a

    Code (csharp):
    1.  
    2. myUMAFFBIKScript.umaCharacter = players[i];
    3.  
    or you can add a function to the umabbfik script where a gameobject is passed as a parameter and it assigns it to the umacharacter reference. Adding a function is preferable as you can do some checking and error handling before the reference is set.
     
  5. frankmat

    frankmat

    Joined:
    Sep 14, 2013
    Posts:
    42
    I tried doing this earlier but for some reason didn't work. For instance I had this in my UMABBFIK.

    Code (CSharp):
    1.    
    2.  
    3. public void setUmaCharacter(GameObject character) {
    4.  
    5.    umaCharacter = character;
    6.  
    7. }
    8.  


    But whenever I tried to call this from gameEngine.cs I was getting issues. I assumed I had done it wrong.

    What would be the best way of assigning say player[4] gameObject to this function within umabbfik.cs?
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    such as?
     
  7. frankmat

    frankmat

    Joined:
    Sep 14, 2013
    Posts:
    42
    Sorry I removed the code because I couldn't get it working... object not set to reference of an object or something similar.

    How would you assign the players[4] gameObject to that function?

    This was what I was told to do... but didn't work.

    gameEngine.cs
    Code (CSharp):
    1.  
    2.  
    3. public UMAFBBIK fbbik;
    4.  
    5. void Update() {
    6. fbbik.gameObject.GetComponent<UMAFBBIK>().setUmaCharacter(players[0]);
    7.  
    8. }
    9.  
     
    Last edited: Jul 11, 2014