Spawn Object based on previous Scene Selection?

Hey Iam working on a Player or better: Car selection sCreen which looks like this at the moment:

2371222--161080--menu_final2.gif

the code for this is the following:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SlotSelect : MonoBehaviour {
     public RectTransform navigator1;
     int nav1Pos = 0;
     public Transform[] cars = new Transform[12];
     public Transform displayAt; // the position where the car is supposed to be displayed
     public Transform selectedCar;// the car which is currently being displayed
    
     public RectTransform[] slots = new RectTransform[12];
     public int jumpAmount = 4;
     public Text textShowNav1;
     void Start(){
         MoveNav1(0);
     }
     void Update () {
         // move up
         if(Input.GetKeyDown(KeyCode.UpArrow)){
             MoveNav1(-jumpAmount);
         }
        
         if(Input.GetKeyDown(KeyCode.LeftArrow)){
             MoveNav1(-1);
         }
        
         if(Input.GetKeyDown(KeyCode.DownArrow)){
             MoveNav1(jumpAmount);
         }
        
         if(Input.GetKeyDown(KeyCode.RightArrow)){
             MoveNav1(1);
         }
         if(Input.GetKeyDown(KeyCode.Return)){
             selectNav1(nav1Pos);
         }
     }
    
     void MoveNav1(int change){
         if(change > 0){
             if(nav1Pos+change < slots.Length-1){
                 nav1Pos += change;
             }else{
                 nav1Pos = slots.Length-1;
             }
         }else{
             if(nav1Pos+change >= 0){
                 nav1Pos += change;
             }else{
                 nav1Pos = 0;
             }
         }
         navigator1.position = slots[nav1Pos].position;
         textShowNav1.text = "Nav 1 is at slot "+ nav1Pos;
     }
     void selectNav1(int nav1Pos){
         selectedCar.gameObject.SetActive(false); //deactivate the previously selected car
         selectedCar = cars[nav1Pos]; // assign the selected car
         selectedCar.position = displayAt.position; //move it to correct position
         selectedCar.gameObject.SetActive(true); // set the car active
     }
    
}

so, my problem is now, how do i get the previous selected car (which is stored in the “public Transform selectedCar;” i think into another Scene? so imagine a Start Game Button with

Application.LoadLevel("Game");

at the bottom which brings you to the next scene with your selected car.

surely you mean DontDestroyOnLoad(selectedCar.gameObject)

Hey, first thanks for the replie

The car in the selectedCar is more a representation. The plan is to load a more complex prefab of the car in the next scene. You know?

There are many possibilities. You can create this complex car in the current scene and then use DontDestroyOnLoad, or create class with necessary settings and in new scene use Instantiate function.

I think the second idea is pretty good

using UnityEngine;
using System.Collections;

public class Instanciate : MonoBehaviour {

    public GameObject prefab;//the corresponding prefab based on the previous scene selection

       void Start() {
        Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity);
       
        }
    }

but how can i fill the “public GameObject prefab;” based on the selection from the prevoius scene?

Add in the awake function of your new class.

void Awake() {
     DontDestroyOnLoad(transform.gameObject);
}

And attach this script to empty game object. (For more info you can check the singleton pattern, which is highly used in game developing)

Or you can just use a static class with static members.

Exactly, how?! When you load next scene all objects will be destroyed, unless you use DontDestroyOnLoad. You can also use PlayerPrefs - additionally you will have save data in case exit app.