Instantiate object based on input

I have spent the entire day wrestling over this one. I am sure it is easier than I am making it.

Using the Laser Defender project from the course,

On the start screen, I would like to have 2 buttons. Ship 1 and Ship 2. OK right. That’s easy.
If Ship 1 is selected than prefab Ship1 is the players’ ship to play the game.

Likewise, if player selects Ship 2 then prefab Ship2 is loaded into the game scene for playing.

I am able to setup a simple instantiation in the game screen and set its position and play.
I am UNABLE to extend this to the start menu and select between 2 ships and have one loaded into the scene and persist through a few playthroughs.

Any insight into setting up the scripts is hugely appreciated. I fear my understanding is not where I want it to be yet.

Hi Gonzo98x,

You could use the PlayerPrefs. Look them up in the API. The two methods you need are SetInt and GetInt.

In the scene, where the player is supposed to choose a ship, there are two buttons. Button 1 would call PlayerPrefs.SetInt("Ship", 0);. In your Game scene, you check the value of “ship” with PlayerPrefs.GetInt("Ship");. Depending on the returned value, you could destroy the wrong ship in the Awake method.

This is probably the simplest way.

Did this help?

Nina thank you so much for this suggestion. It worked very well indeed to create a value I can test for throughout the game and instantiate a prefab based on this value.

I hope one day to learn enough that I can pay it forward to the community.

I’m glad I was able to help. Feel free to answer all questions in our community you can answer. Don’t wait until you learnt “enough”. There is no “enough” because game development requires lifelong learning. :wink:


See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.