- Home /
Instantiate problems
HI, i try to instantiate an object from another scene, but the object never showed up because NullReferenceException
so basically i have 2 scene, CharSelection and GameWorld. here's the code that link them
this is CharacterSpawn.js that i attach to "GameWorld", MainCamera
var playerMale : GameObject;
var playerFemale : GameObject;
var savedplayer : int = 0;
function Awake () {
savedplayer = PlayerPrefs.GetInt("selectedplayer");
playerMale = GameObject.Find("Cube1_M");
playerFemale = GameObject.Find("testchar_F");
if(savedplayer == 1){
Instantiate(playerMale, Vector3(2,2,2), Quaternion.identity);
playerFemale.SetActive(false);
gameObject.AddComponent("PlayerStats");
}
else if(savedplayer == 2){
//Instantiate(playerFemale, Vector3(0,0,0), Quaternion.identity);
playerMale.SetActive(false);
//playerFemale.SetActive(true);
gameObject.AddComponent("PlayerStats");
}
}
this is ChangeChar.js attached to option button on "CharSelection"
var isMale = false;
var isFemale = false;
var selectedplayer : int = 0;
function OnMouseUp() {
if(isMale == true) {
PlayerAttributes.player_gender="Male";
print(PlayerAttributes.player_gender);
GameObject.Find("Cube1_M").transform.position.x = -1.7;
GameObject.Find("testchar_F").transform.position.x = -7;
selectedplayer = 1;
PlayerPrefs.SetInt("selectedplayer", (selectedplayer));
}
if(isFemale == true) {
PlayerAttributes.player_gender="Female";
print(PlayerAttributes.player_gender);
GameObject.Find("Cube1_M").transform.position.x = -7;
GameObject.Find("testchar_F").transform.position.x = -1.7;
selectedplayer = 2;
PlayerPrefs.SetInt("selectedplayer", (selectedplayer));
}
}
when i Instantiate i need playerMale as an GameObject.Find (the object to instantiate) but the GameObject.Find() need the object on the scene to point, and because my object was on CharSelection scene, it shows an error, which is NullReferenceException..
is there any way out of this loop?
Answer by WilliamLeu · Dec 03, 2013 at 07:24 PM
If it's a template GameObject that's always the same, I'd suggest referencing a Prefab. They're game objects that are saved in the file hierarchy. You can assign them to GameObject references in the inspector like scene GameObjects. Their purpose is specifically to have Instantiate() called on them.
If it's a unique character that was created at runtime from another scene before the new scene was loaded, you could use DontDestroyOnLoad() on the GameObject. From there you can either call Instantiate() on it, or just reuse that existing GameObject that was created - but not destroyed - from the previous scene(s).
yeah, you got the point, i want to use the character that was on another scene... whic is character creation scene..
therefor i use DontDestroyOnLoad()..
so i dont need to use Instantiate anymore?
and also, if i use DontDestroyOnLoad(), does the object appear somewhere on the scene? can i add some attribute after the selection?
oh, and where should i put DontDestroyOnLoad()? last time i put it on the character.. is it ok?
It doesn't matter when or from what script you call DontDestroyOnLoad() from, as long as it's called before the next scene is loaded and as long as it's called with the GameObject as the parameter that you want to keep from being deleted.
You don't need to use Instantiate() as long as you don't need to clone more of them.
The object should be where it last was, wherever its Transform behavior says it is. I'm guessing it will be kept where it was in world space before the new level loaded.
Your answer
Follow this Question
Related Questions
Plant Reproduction, Instantiate problem 1 Answer
How can I replace an object while keeping the references to that object? 2 Answers
Instantiated enemies not adding to kill count 1 Answer
Problem with Unity 0 Answers
Can't select shader help! 1 Answer