- Home /
Boolean check not referenced correctly from other script
Hello - in my game the player can switch between the first person controller and the third person controller on pressing the space bar.
On hitting a collider however, after three seconds it is switched to the first person controller automatically (without the player pressing space)
This works - but for some reason you have to press spacebar twice in order to switch back to the third person controller. I don't think I'm referencing the boolean check from the SwitchControllers script correctly - but I can't work out what I'm doing wrong.
This is the ForceIntoFirstPerson script (JavaScript):
var cam01 : GameObject; // first person camera
var cam02 : GameObject; // third person camera
var player01 : GameObject; //first person controller
var player02 : GameObject; //third person controller
public static var check : boolean = false;
function OnTriggerEnter(other: Collider){
if (other.tag == "Player")
{
yield WaitForSeconds (3);
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player01.active = true;
player02.active = false;
check = true;
}
}
This is the Switch Characters script:
var cam01 : GameObject; // first person camera
var cam02 : GameObject; // third person camera
var player01 : GameObject; //first person controller
var player02 : GameObject; //third person controller
public static var check; // check-variable
//start with first person active
function Start() {
check = true;
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player02.active = false;
//check = true;
}
function Update() {
player01.transform.position = player02.transform.position;
if (Input.GetKeyDown ("space")) {
if(check) {
cam01.gameObject.active = false;
cam02.gameObject.active = true;
player01.active = false;
player02.active = true;
}
else {
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player01.active = true;
player02.active = false;
}
check = !check;
}
}
I tried this alteration (amongst many others), but it didn't work.
if (check) check = false; else check = true; }
Any help would be so, so much appreciated!! (I'm stuck until I sort this one out, and deadlines are scarily close)
Thanks so much, Laurien
Answer by perchik · Aug 29, 2013 at 04:41 PM
Well you are correct, your boolean check is not being referenced correctly.
In your ForceIntoFirstPerson script, you need to have a reference to the switchCharacters script and set the 'check' variable of the switchCharacters script.
I'm not entirely sure of the Javascript version, but in C# it would be something like
switchCharacters switchScript;
Start(){
...
switchScript = GetComponent<switchCharacters>();
}
...
switchScript.check = true;
Javascript is probably:
var switchScript: switchCharacters;
Start(){
switchScript = GetComponenet<switchCharacters>();
switchScript.check= true;
}
Your other syntax, check =!check
does exactly what your alternative says.
Hi - I've formatted it to this in the ForceIntoFirstPerson script - it doesn't throw any errors, but I still have to press spacebar twice after hitting the collider to get back to the first person controller.
In the error log, on pressing the spacebar once it says Null Reference Exception: Object reference not set to an instance of an object. It refers to this line : switchScript = GetComponent(SwitchCharacters).check;
var cam01 : GameObject; // first person camera
var cam02 : GameObject; // third person camera
var player01 : GameObject; //first person controller
var player02 : GameObject; //third person controller
public static var check : boolean = false;
var switchScript : SwitchCharacters;
function Start () {
switchScript = GetComponent(SwitchCharacters).check;
switchScript.check = true;
}
function OnTriggerEnter(other: Collider){
if (other.tag == "Player"){
yield WaitForSeconds (3);
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player01.active = true;
player02.active = false;
check = true;
}
}
Is the variable being declared wrong do you think?
Don't use the local check at all in that script, when you said check=true
that needs to be switchScript.check =true
also, this line switchScript = GetComponent(SwitchCharacters).check;
needs to lose the .check at the end.
The reason it works when you press space twice is that the first time you press space, check is false so it gets changed to true. The second time you press space, check is true and the code runs. This means that it's not actually getting changed from the script you just included.
Ok - thanks! I've changed it to this now, but I'm still having the same problem. I also still get the error Null Reference Exception as soon as the player is forced into first person - now at line 28 : switchScript.check = true;
var cam01 : GameObject; // first person camera
var cam02 : GameObject; // third person camera
var player01 : GameObject; //first person controller
var player02 : GameObject; //third person controller
var switchScript : SwitchCharacters;
function Start () {
switchScript = GetComponent(SwitchCharacters);
switchScript.check = true;
}
function OnTriggerEnter(other: Collider){
if (other.tag == "Player"){
yield WaitForSeconds (3);
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player01.active = true;
player02.active = false;
switchScript.check = true;
}
}
I've tried it in function Update already - but that doesn't work either.
Answer by ThermalFusion · Aug 29, 2013 at 05:37 PM
The static variable 'check' in the two scripts are not the same variable, but two different. You probably mean to change SwitchCharacter's check in both scripts. Static variables still belong to the class they're declared in. Access them using ClassName.variableName. So in your case: SwitchCharacters.check = !SwitchCharacters.check.
I've reformatted to this- but it's not working yet. I think it's the line switchScript.check = true;
that isn't working, as I'm getting the error Object reference not set to instance of an object.
var cam01 : GameObject; // first person camera
var cam02 : GameObject; // third person camera
var player01 : GameObject; //first person controller
var player02 : GameObject; //third person controller
var switchScript : SwitchCharacters;
function Update () {
var switchScript : SwitchCharacters = gameObject.GetComponent(SwitchCharacters);
switchScript.check = true;
}
function OnTriggerEnter(other: Collider){
if (other.tag == "Player"){
yield WaitForSeconds (3);
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player01.active = true;
player02.active = false;
switchScript.check = !switchScript.check;
}
}
Any ideas? (thanks so much!)
I've made the change you suggested ThermalFusion - but it hasn't fixed it.
Your answer
Follow this Question
Related Questions
Script that switches between first and third person controller 3 Answers
Switching between controllers - why do I have to press spacebar twice before it works? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Reference boolean check from other script (not working) 1 Answer
How to Get My Camera to Follow First Person POV in First Person Controller 1 Answer