accessing a gameobjects gameobject variable

Hi, this is a very wordy problem so I’m sorry if my initial explanation is lacking but I instantiate an object in a script and then set that object to a game object in the scripts game object array. I want to access the game object that has just been instantiated into that game object slot variable in another script. I’m starting to consider whether this is possible.

Sure, there are many ways to do this.

You could expose the array as a public field on the first script. If you’re trying to figure out how to tell which array entry holds the new object, just add another public property – either the array index where the new object was stored, or a reference to the new object itself.

(Note it’s probably better to use a collection type, arrays have many limitations and few benefits.)

The second script would use GetComponent to retrieve RobotManager from wherever it lives, then after calling MakeAnotherRobot, the newObj would point directly to the newly-created object, newIndex would point to the array slot where the new object is stored, and objArray would be the entire array of all robots created so far.

using UnityEngine;

public class RobotManager : MonoBehaviour
{
    public GameObject robotPrefab; // set in inspector

    // you could put HideInInspector on these public fields
    public GameObject[] objArray;
    public int newIndex;
    public GameObject newObj;

    private int nextIndex; // where to store a new object

    void Awake()
    {
        // initialize the array
        objArray = new GameObject[20];
        nextIndex = 0;
    }

    public void MakeAnotherRobot()
    {
        if(nextIndex == 20) return; // don't exceed array size
        newObj = Instantiate<GameObject>(robotPrefab);
        objArray[nextIndex] = newObj;
        newIndex = nextIndex++;
    }

}

sorry, I don’t think I made what I wanted to do clear enough, here is my array that is storing cards in it game object array in one script and how it is creating the objects for this array

    public void CreatePlayersUnit()
    {
       CUnitStats = UnitParemeters.GetComponent<Unitstats>();
       CResourceCard = CardParemeter.GetComponent<ResourceCard>();
       if (CanvasCard == 0)
       {
       
        CUnitStats.Cardid = 1;

        CUnitStats.Cardname = CResourceCard.Cardname;
        CUnitStats.Cardpower = CResourceCard.Cardpower;
        CUnitStats.Carddefense = CResourceCard.Carddefense;


        if (CUnitStats.CardName) CUnitStats.CardName.text = CUnitStats.Cardname;
        if (CUnitStats.CardPower) CUnitStats.CardPower.text = CUnitStats.Cardpower.ToString();
        if (CUnitStats.CardDefense) CUnitStats.CardDefense.text = CUnitStats.Carddefense.ToString();

        PlayerCard[0] = Instantiate(ResourceviewCard, transform.position, Quaternion.identity) as GameObject;
        PlayerCard[0].transform.SetParent(CardPannel,false);
        Debug.Log("this is card id after creation set-up" + CUnitStats.Cardid);
        CanvasCard = CanvasCard + 1;

       }

and here is where I am trying to manipulate the object in the object array in another script:

 CDeck = GameObject.Find("ResourceDeck").GetComponent<ResourceDeck>();

   if (NotOrganised == true)
                {
                   // if (CDeck.PlayerCard[CDeck.CanvasCard - 1] != null) // if there is another card
                 // {
                      CDeck.PlayerCard[Cardid-1].cardpower = CDeck.PlayerCard[CDeck.CanvasCard - 1].cardpower;
                      Destroy(CDeck.PlayerCard[CDeck.CanvasCard - 1]);
                    
                      NotOrganised = false;
                      Cardid = 1; 
                 // }
                 // else { }
                    //Destroy(this.PlayerCard[Cardid - 1]);
                  NotOrganised = false;
                  Usabiality.CardsUsed = Usabiality.CardsUsed - 1;
                  CDeck.CanvasCard--; 
                  Debug.Log("I have been clicked for " + Usabiality.CardsUsed); 
                }

the key line being

CDeck.PlayerCard[Cardid-1].cardpower = CDeck.PlayerCard[CDeck.CanvasCard - 1].cardpower;

being the code which is supposed to set one of the elements stats to another elements stats

Is the problem that you need to get Cardid, the value in the line 21 Debug.Log in the first script?

It looks like your second script just needs to also grab a reference to Unitstats and use that reference to access Cardid, no?

sorry the debug was there just so I knew exactly which card I was clicking on since the card id is used to differentiate between the cloned objects.

wouldn’t the reference to the game object in the array not contain the reference to the game object itself?

so that line of code should get the game object in the area at that id and set its power stat to the last card in the array

Oh, is the first script from ResourceDeck? The code you posted doesn’t show that.

If so, then I think this is what you’re trying to do:

CDeck.PlayerCard[CDeck.CUnitStats.Cardid-1].cardpower = CDeck.PlayerCard[CDeck.CanvasCard - 1].cardpower;

You still have to tell C# where to find Cardid, just like you did with CanvasCard.
And that assumes CUnitStats is public inside the deck class.

sorry I guess I just did say "one script " and didn’t clarify it. so the CUnits would be the object reference in the resource deck script and a new object reference would not be needed to be defined in the script I’m using to manipulate the object

to try and simplify what I’m trying to do I have made a basic debug function to access the variable I want to access however I have an error thrown:

        Debug.Log("the card id is " + CDeck.PlayerCard[0].CUnitStats.Cardid);

"UnityENgine.GameObject[ ] does not contain definition for “CUnitStats” and no extension method “CUnitStats” of type UNityEngine.GameObject[ ] could be found

The error tells me PlayerCard is a GameObject array (and that’s what you Instantiate, a GameObject).

Based on the CreatePlayersUnit function, both PlayerCard and CUnitStats are part of the ResourceDeck class.

It looks like you need a way to store CUnitStats in PlayerCard (or maybe just the ID, depending on what you’ll do later on). So probably you need to add a script to your ResourceviewCard (I’m guessing that’s a prefab?) then after you instantiate it, set CUnitStats or ID or whatever.

Then when you use it in the second script, you’ll have to GetComponent on PlayerCard for that new script.

There are other ways to do this, though. Probably I’d make PlayerCard some kind of class and store an array of those, with all the other information you need, along with a GameObject reference that comes from Instantiate. That way you don’t have to do GetComponent all the time.

Some ocd part of me is forced to say - not necessarily true. Arrays of a fixed sized have many good uses, where you know ahead of time exactly how big a collection is going to be, and that it’s size won’t be changing, and cases like that wouldn’t really require a generic list or anything…

Although the way this thread is worded I wonder if your not right in this particular case, because it seems he may instantiate more of these gameobjects at runtime, but anyway had to say it :stuck_out_tongue:

Hence “few” benefits… and they do absolutely have many limitations.

They do, but lets not say to people “never use these” :stuck_out_tongue:

I use em all the time for simple collections, haven’t had any big issues!