- Home /
The question is answered, right answer was accepted
NullReferenceException: When accessing bools from another game object's script.
So I have this code below. What it does is simulate the floor moving by offsetting the texture material at a certain pace. That is not important, however, as my problem lies within line 16. I have a bool "dead" as a variable in my Platformer2DUserControl.cs. It is attached to a fish. Everything moves with the fish as he goes through the level (that is why I have the texture offset, so it looks like the floor is stationary). I want this floor scrolling to stop when the fish dies because the fish stops when he dies.
Everything actually works out as planned. As the fish moves, the floor texture scrolls backwards and it looks like the fish is moving, when he dies the floor stops, and looks like I am not moving. But I still get this error.
EDIT: Okay, the floor is not actually scrolling. So nothing is happening after if(!Fish.dead) Is this not how you access a bool variable from another script?
using UnityEngine;
using System.Collections;
public class floorscroller : MonoBehaviour {
public float speed;
Platformer2DUserControl Fish;
void Start(){
Fish=GetComponent<Platformer2DUserControl>();
}
void Update ()
{
if(!Fish.dead){ //This is the line with the error (Line 16)
renderer.material.mainTextureOffset = new Vector2((Time.time * speed)%1, 0f);
}
else
{
renderer.material.mainTextureOffset = new Vector2(0f, 0f);
}
}
}
Are you destroying the fish when he dies? If so your 'Fish.dead' reference is bad.
I thought of that, and the collider script (the only thing that would kill the fish) just changes the the Fish' swim animation to dead. No where do I destroy the object. In fact, he just turns blue and goes belly up, then floats to the top of the screen. (I will have to double check to make sure nothing is getting destroyed though)
The odd part is everything works fine. The texture stops scrolling, the fish enters his dead animation. The camera is set to follow the fish, and most objects are attached to the camera, so everything stops when the fish collides, but he is still there, just in his dead animation.
Okay, the floor is not actually scrolling. So nothing is happening after if(!Fish.dead) Is this not how you access a bool variable from another script?
I got it to work, as you can see in my answer. however, I do not know why it happened, or why what I did eventually worked. Could it be because I was assigning the variable Fish as a Platformer2DUserControl (referencing the script of that name). But the actual variable I was using was a bool?
Answer by Dblfstr · Feb 04, 2014 at 11:19 PM
// Figured it out, should have just called the script like so.
using UnityEngine;
using System.Collections;
public class floorscroller : MonoBehaviour {
public float speed;
void Update ()
{
if(!Platformer2DUserControl.dead){ //This is the line with the error (Line 16)
renderer.material.mainTextureOffset = new Vector2((Time.time * speed)%1, 0f);
}
else
{
renderer.material.mainTextureOffset = new Vector2(0f, 0f);
}
}
}
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a light turn on and off with the same button 1 Answer
How do I stop jumping in midair? 4 Answers
how to use bool from another script in if statment? 2 Answers