Having different objects to instantiate and switch between them.

First of all, i’ve been reading the forums for a while now but this is my first ever post. So i hope i did everything okay.

So i’m running into a problem. How would i make a system that changes a active prefab through buttons?
For example: you have 8 tree’s and you have a function that instantiates a tree at your position. But now you want to switch between these tree’s (though UI Buttons). So if i click button 2 it needs to instantiate tree 2 when i click, and if i click button 6 it needs to instantiate tree 6 when i click. Etc.

Some basic techniques or guidelines would be really nice. So i atleast can get this system going.

Are you talking about an editor tool here, or is this a runtime thing that players would be doing?

1 Like

Sorry mybad, its a runtime tool. The player starts the game is able to place an object and has multiple buttons on the left side through which he/she should be able to switch between the right object to place down. It’s for a Mobile AR app using the Google Tango. It’s about placing furniture inside of your living room etc. So you should be able to place like 9 different beds for example

I am typing this on the phone so excuse the formatting.

If I understand you correctly you wish to instantiate a gameobject from a prefab?

Look up the Resources.Load method

In the editor place the prefab in folder Asset/resources.

Let’s say you named them tree1 tree2, etc

In the Start section of your script you

 GameObject treeSource =

Uh ok that looks weird on my phone, the code is cut off

I know what you mean, that might help indeed thankyou! Will look into it right away. But doesnt that require me to make a huge amount of code still? I’m trying to code as little and as nice as possible. But my understandig is, i should change the gameobject (for example treeSource) through the Resources.Load and than just always instantiate TreeSource?

So i got the following working: standard the object is set in start void. when i click the button it changes to object 2. But how would i go and code the other objects. Without using an ugly amount of code. I need some sort of way to say when i click this button resources.load is the following. And i got this working but only for 1 object atm. How would i nicely code it for multi objects? I got this atm:

 Instantiate(TestObject, planeCenter, Quaternion.LookRotation(forward, up));
    public void ButtonTest() {
        TestObject = Resources.Load("Prefabs/Cube") as GameObject;
    }

You could use a method with a for loop like:

public ButtonTest(params string[] objectNames){
GameObject[] gObjects = new GameObject[objectNames.Length];
for(int i = 0; i < objectNames.Length; i++){
gObjects[i] =  Resources.Load("Prefabs/" + objectNames[i]) as GameObject;
}
}

Thankyou for your help! However i already found another way. Using a seperate script which changes the activeobject (object to be place down) with it’s own gameobject. I put this scrip on every button and drag in the right prefab and onclick it changes. This way everytime i make a new button al i have to do is drag the script on and drag the prefab in. Later on i will make it so it loads in from a database. Thanks anwyay! Hope other people who find this thread will use your solution.

can you help me with that ? what script do you use to put in prefabs