- Home /
Question by
gmraju333 · Apr 18, 2012 at 01:04 PM ·
gameobjectprefabload
how to load gameobject from one scene to other?
i am having 2 game objects as male and female. by clicking on button i am getting that particular object in first scene. after in second scene i want to start the game by that selected game object. so how to get that previous selected game object to the second scene?
Comment
Answer by vfxjex · Feb 08, 2016 at 06:08 AM
better to use static variables.
using UnityEngine;
using System.Collections;
public class LoadSelected : MonoBehaviour {
// store your male female chaters here using the inspector
public Transform[] characters;
// this will store what character you have selected
static Transform charSelected;
// Call this function on Unity UI Button and initialize an intiger
// for the first button make it 0 and second button 1
void CharSelection (int charNth){
charSelected = characters [charNth];
}
}
your second script goes like this when loading the game
using UnityEngine;
using System.Collections;
public class LoadCharacter : MonoBehaviour {
Transform myChar;
void Awake (){
myChar = Instantiate (LoadSelected.charSelected) as Transform;
}
}
you can add movement and rotation on your Instantiate if you want to
Answer by BiG · Apr 18, 2012 at 01:07 PM
Using DontDestroyOnLoad, applied to both your objects. When you load one of them in the first scene, it will persist in the scenes that you will load in a second time.