- Home /
[Closed]NullReferenceException on Object Instantiation onto game world
Here's my code
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 == 0){
}
else if(savedplayer == 1){
playerMale.SetActiveRecursively(true);
playerFemale.SetActiveRecursively(false);
}
else if(savedplayer == 2){
playerMale.SetActiveRecursively(false);
playerFemale.SetActiveRecursively(true);
}
}
function Update () {
}
I put that code on the Main Camera on Game Scene.
and this code goes to an object to choose Character (Male of Female) on CharSelection Scene.
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 = -5;
selectedplayer = 1;
PlayerPrefs.SetInt("selectedplayer", (selectedplayer));
}
if(isFemale == true) {
PlayerAttributes.player_gender="Female";
print(PlayerAttributes.player_gender);
GameObject.Find("Cube1_M").transform.position.x = -5;
GameObject.Find("testchar_F").transform.position.x = -1.7;
selectedplayer = 2;
PlayerPrefs.SetInt("selectedplayer", (selectedplayer));
}
}
but when i press button to load the Game Scene, the object did not appear at the scene, and the error is NullReferenceException on ActiveRecursively function...
could anybody help me out?
Are Cube1_$$anonymous$$ and testchar_F active at the beginning ? If not, GameObject.Find("Cube1_$$anonymous$$")
will return null
When you have an error, you should tell us at what line is the error. It's easier to find the problem.
yeph it is active, and currently be my object on CharSelection scene.. the line that eror is
player$$anonymous$$ale.SetActiveRecursively(false); playerFemale.SetActiveRecursively(true);
depends on what my char is (male or female) but the error is there, setactiverecursively(true); i confused what i wrong.. could u help ?
btw thanks for answering :)
Try just SetActive ins$$anonymous$$d of SetActiveRecursively because it is deprecated. It will unable by propagation every child. If it is not the behavior you are looking for, we'll find something else.
i've try to change it, but the problem is still the same, maybe it have something to do with var i create? want to check out my unity project? (if u 're not busy)
or maybe there is another way to instatiate character to game from character selection screen?
Answer by KiraSensei · Nov 28, 2013 at 07:48 AM
You just need to create a new script with :
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
In it, and attach it to the "Character" game object, because it contains the two game objects you need. Or you can attach the script directly to Cube1_M and testchar_F, this should work too !
Answer by raimon.massanet · Nov 27, 2013 at 03:39 PM
GameObject.Find
is something you should not use unless you have no other option. Instead, expose playerMale
and playerFemale
in the inspector, then drag and drop the objects from the scene.
could u explain me "expose player$$anonymous$$ale
and playerFemale
in inspector"? ermmhh, quite not understand thou.. sorry, it seems still a lot of thing i need to learn :)
He says that you need to declare them public, and they will appear in the inspector, after that you just have to drag and drop the corresponding game object into the correct line in the inspector. But this works only if they already exist in the scene. If you instantiate them with another script, you need to use GameObject.Find
Exactly. Unity's way of working is like that. You should drag and drop in the Inspector window as much as you can. If the players are not in the scene, but ins$$anonymous$$d they are prefabs, it's O$$anonymous$$, you can drag and drop a prefab too and then Instantiate it.
so i can still instantiate it with drag and drop? so i create new var for player$$anonymous$$ and playerFwith type of GameObject?
thanks for explaining so far :)
Your answer
Follow this Question
Related Questions
After Instantiating a Prefab I get a NullReference Error even though the Object was created 2 Answers
NullReferenceException - When destroying Object 1 Answer
Error putting an object into an Array 0 Answers
NullReference Exception when adding a class 2 Answers
Instantiate GameObjects, brings an error to the log... 2 Answers