- Home /
Referencing an instantiated object with a global variable
I'm instantiating an object and plan to destroy it in another function, so i'm trying to reference it through a global variable. I'm currently doing
void DrawScore() {
GameObject a;
GameObject b;
GameObject c;
Vector3 star1Pos = GUIdrawCentre - new Vector3(+GUIdrawOffset,0,0);
Vector3 star2Pos = GUIdrawCentre;
Vector3 star3Pos = GUIdrawCentre - new Vector3(-GUIdrawOffset,0,0);
if(starCount == 0)
{
}
if(starCount == 1)
{
a = Instantiate(GUIstar, star1Pos, Quaternion.identity) as GameObject;
_stars[0] = a;
}
}
then in another scripts, looping through the array and destroying each member.
_stars[] is an array of GameObjects declared globally
For whatever reason, _stars[0] is always set to "none" and never gets overwritten with anything.
Any ideas why this is happening? I've used the same method in other projects but this time it just isn't working
Your answer
Follow this Question
Related Questions
Destroying 2D Array of Instantated Objects 2 Answers
using destroy with an array of GameObjects (C#) 1 Answer
How can I destroy objects and instantiates new in Array? 2 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Instantiate from array into array? 2 Answers