Link to home
Start Free TrialLog in
Avatar of James Hancock
James HancockFlag for United States of America

asked on

Unity game engine - Must my GameObjects exist on the scene for them to be found in code?


In my Unity Game project, I've put all the GameObject / sprites my code needs to access on the right third of the scene, off the game window, ( a vertical iPhone/device intended game, for now) cause it seems to be the only way for the interpreter to find them. Is this okay? They are letter tiles. I have 26 alphabet letter tiles and 26 selected letter tile images littered around, unseeable in-game. My code will need to acquire them in my start()  at least for this question,  26 letter tiles,  something like this:

for (int t2 = 0; t2 < 26; t2++)
               LetterTiles[t2] = null;
               LetterTiles[t2] = GameObject.Find("letter" + t2);

                if (LetterTiles[t2] == null) break;
                if (LetterTiles[t2] != null) print("------Tile letter" + t2 + " FOUND!!!!");

Open in new window

I'm not truly concerned, coz it works, but is this a good or bad way to do it?


Avatar of louisfr

I've read some documentation but I'm no expert in that field. So this is only my thoughts until someone more in the know answers.

I guess you could use Resources.Load when you need your sprites, or load them at start and store them in a Dictionary instead of in the scene.
I think you can use Instantiate to put the object in the scene when you it.
Avatar of James Hancock


So my loading code, now:
GameObject letterBoard1 = GameObject.Find("letterBoard1");

would be indistinguishable to:
GameObject letterBoard1 = Resources.Load("some/directory/letterBoard1.gif");
or similar?
I'm not sure, because objects need to have colliders or components attached to them, so is loading a resource like that only the beginning?
Avatar of James Hancock
James Hancock
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial