- Home /
Accessing variable from another script
Holla !
im trying to access a variable from another script, but i just couldn't get it done.. no matter how i do, the error is always Object reference not set to an instance of an object
so,, here's my code
chgChar.js
var isMale = false;
var isFemale = false;
public var selectedplayer : int = 0;
static var player_gender : String = "Female";
function OnMouseUp() {
if(isMale == true) {
player_gender="Male";
print(player_gender);
GameObject.Find("Boy").transform.position.x = -5;
GameObject.Find("Girl").transform.position.x = -25;
selectedplayer = 1;
//PlayerPrefs.SetInt("selectedplayer", (selectedplayer));
}
if(isFemale == true) {
player_gender="Female";
print(player_gender);
GameObject.Find("Boy").transform.position.x = -25;
GameObject.Find("Girl").transform.position.x = -5;
selectedplayer = 2;
//PlayerPrefs.SetInt("selectedplayer", (selectedplayer));
}
}
charSpawn.js
var female : GameObject;
var male : GameObject;
var targetscript : chgChar;
var selected : int;
function Start () {
targetscript=GetComponent.<chgChar>();
selected = targetscript.selectedplayer;
if(selected==1){
Instantiate(male,Vector3(0,0,0),Quaternion.identity);
}
else if(selected==2){
Instantiate(female,Vector3(0,0,0),Quaternion.identity);
}
}
anyone know what my doing was wrong? btw the error is on this line
selected = targetscript.selectedplayer;
A few suggestions:
This line:
if(chgChar.selectedplayer==1){
should probably be:
if(selected==1) {
You are currently trying to access an instance variable through a class. Same thing goes for the if else
statement underneath it.
Also, make sure there is chgChar
attached to the same object as the charSpawn
, and you should use the generic version wherever you can:
targetscript = GetComponent.<chgChar>();
for performance and type-safety's sake.
thanks @Peter G yeah i have change it now.. but i dont udnerstand, u mean by attached to the same object, that 2 scripts must come from same object? i must atttach it to same 3d object?
but the chgChar is one char selection scene, while the charSpawn is in the game scene where i will spawn the player..
Probbably your chgChar.js and charSpawn.js is not on the same object. Therefore first you will have to find on which object your chgChar.js attached to. So try to correct this line first:
lets say that your chgChar.js is on an object named Character and your Character's tag is Char then you can find the object with 2 methods:
targetscript=GameObject.Find("Character").GetComponent<chgChar>();
//OR
targetscript=GameObject.FindGameObjectWithTag("Char").GetComponent<chcChar>();
I have provided C# codes since I'm not familiar with JavaScript. But you should get the idea behind it.
Answer by robertbu · Dec 13, 2013 at 07:29 AM
Need '()' at the end of GetComponent:
targetscript=GameObject.Find("Character").GetComponent<chcChar>();
The Javascript version of get component:
targetscript=GameObject.Find("Character").GetComponent(chcChar);
@robertbu hi robert ! i've tried to change it to GameObject.Find("genderBoy").GetComponent(chgChar); but the problem is still the same,
targetscript=GameObject.Find("genderBoy").GetComponent(chgChar);
NullReferenceException: Object reference not set to an instance of an object charSpawn.$$anonymous$$ain () (at Assets/testing script/charSpawn.js:8)
Spelling must be exact. 'genderBoy' != 'GenderBoy' != 'Gender Boy'... And the 'chgChar' component must be on the game object with the name you specify. If you have more than one game object with the name 'genderBoy', check to make sure they all have the 'chgChar' script attached.
so, basically, i have 2 object too choose wether the char is boy or girl, its 2 different object if i click genderBoy, the char shown will become boy, and if i click genderGirl, the char shown will become girl. and this 2 object are in charSelection scene.
and i have 1 scene called GameWorld, where i want to spawn my boy/girl there, which why i put charspawn.js on here, at GameWorld scene..
that's why it will be so weird for me if i should find GameObject where chgChar.js is on.. becase it's on different scene..
i know it is confusing.. u want me to sent u my project ins$$anonymous$$d?
Pretty confusing situation. Are you sure that 'genderBoy' exists at the time you are executing this code? Try executing this just before you do the get component call:
var go = GameObject.Find("genderBoy");
Debug.Log(go);
i dont think it will come up with anything since they are on different scene?
but i did tested it and
null UnityEngine.Debug:Log(Object)
that's what the debug says..
emm, i dont think u quite get what i want.. basically i want to spawn the character, which i choose on another scene.. so that's why the charSpawn is on another scene..
Your answer
![](https://koobas.hobune.stream/wayback/20220613124025im_/https://answers.unity.com/themes/thub/images/avi.jpg)