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?


Game Programming* UnityC#JavaScript

Avatar of undefined
Last Comment
James Hancock

8/22/2022 - Mon

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.
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?
James Hancock

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck