checking other object variable
Hi people,
I would like to ask if it is possible to check a variable residing in another object.
I have already searched through the unity script reference, but it doesn't answer the question http://docs.unity3d.com/410/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
I don't want to call a function and i don't want to add,subtract, or translate, addforce etc.
I would like to check if a bool on object A is true or false so that object B may react accordingly
object B will be spawned many times and at many different intervals while Object A is always there until it collides with object B and Object A's bool is true.
The trick is that the player will be changing the bool at will.
So to summarise the question.
How do i get Object B to check if Object A's bool is true or false on contact?
Answer by CanCo · Jul 16, 2016 at 05:47 AM
Say your first script is just
public class code1 : MonoBehaviour {
public bool foo = true;
}
then you'll need another code which is like:
public class code2 : MonoBehaviour {
public code1 _code;
void Start () {
if(_code.foo == true)
doSomething();
}
}
(This is written in C#)
it always returns this error
NullReferenceException: Object reference not set to an instance of an object ContactDeath.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/ContactDeath.cs:32)
The line of code that it is referring to is this
if(colorChanger.isRed == true)
Just figured out a solution.
not 100% on why what you said didn't work
but because it was an on trigger enter script i just used
if(other.GetComponent<ColorChanger>().isRed == true)
I'm going to give you an upvote though because that is how it is supposed to work.
Remember that if you get a component of another object during runtime the program will be slower, so define a variable to hold the value of the ColorChanger class.
Your answer
![](https://koobas.hobune.stream/wayback/20220612084928im_/https://answers.unity.com/themes/thub/images/avi.jpg)