Need help assigning GameObjects via script.

Hello, everyone!

I have a Controller GameObject. It has an attached script where an object will be instantiated. I have declared it in my script and it works when I manually drag the object to the inspector. So far, i have set just one object to be instantiated. However, i wanna modify it so that when i show image A, obj A will be instantiated and when I show image B, obj B will be instantiated. Everything is in my hierarchy and are just currently hidden unless i call them. I just need to be able to assign objects via script instead of dragging them manually. Thanks in advance.

There are two approaches to do this …

  1. Make prefabs of object you want to instantiate , save them in Resources folder and load using Resources.Load as you need them …
  2. Makes an public array of GameObject , assign your objects to it and instantiate as you need using indexes.

Make condition/switch statement to check active image.

I use personnally GameObject.Find(“name”), but it requires to destoy the preceding objects so I’m not sure you wish this.