- Home /
How to use a boolean to control another object boolean using C# script
Hi, I have a control panel and i want to use it to control the animation of a landing gear.. So this is the script i have written for the control panel which i can control using keydown and..
public class panel : MonoBehaviour {
public bool stick;
private Animator animator;
private GameObject player;
// Use this for initialization
void Start () {
animator = GetComponent<Animator> ();
}
// Update is called once per frame
void OnTriggerStay(Collider other) {
if (Input.GetKeyDown (KeyCode.P)) {
animator.SetBool ("stick", true);
}
if (Input.GetKeyDown (KeyCode.O)) {
animator.SetBool ("stick", false);
}
}
this is the script i written for landing gear which i want it to detect the boolean is true or false from the control panel:
public class landinggear : MonoBehaviour {
private Animator anim;
private GameObject player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(GameObject.Find("panel").GetComponent<panel>().stick)
{
anim.SetBool("retraction",true);
}
if(!GameObject.Find("panel").GetComponent<panel>().stick)
{
anim.SetBool("retraction",false);
}
}
}
So both object have its own animator, and i succeed to activate control panel but not the landing gear.. and I very new to C# scripting so anyone can stop by and help me with this.. thank you very much.
Really appreciate for the answer @corn @SterlingSoftworks, so i have do some editing but still come out with this error:
NullReferenceException: Object reference not set to an instance of an object landinggear.Update () (at Assets/landinggear.cs:26)
script for panel :
public class panel : $$anonymous$$onoBehaviour {
public bool stick;
private Animator animator;
private GameObject player;
// Use this for initialization
void Start () {
animator = GetComponent<Animator> ();
}
// Update is called once per frame
void OnTriggerStay(Collider other) {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.P)) {
animator.SetBool ("stick", true);
stick = true;
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.O)) {
animator.SetBool ("stick", false);
stick = false;
}
}
}
script for landing gear:
public class landinggear : $$anonymous$$onoBehaviour {
private panel pan;
private Animator anim;
private GameObject player;
// Use this for initialization
void Start () {
pan = GameObject.Find ("panel").GetComponent<panel> ();
}
// Update is called once per frame
void Update () {
if(pan.stick)
{
anim.SetBool("retraction",true);
}
if(!pan.stick)
{
anim.SetBool("retraction",false);
}
}
}
p/s : I still very confuse with using code to call upon checking for the panel boolean is it true or false ( meaning to say under landing gear script ; under the void update ; that part i not sure is it a correct way to tell the landing gear check whether panel boolean is true or false..) $$anonymous$$indly please help me with that.. thanksss
Answer by SterlingSoftworks · Jan 21, 2016 at 11:19 PM
The problem is that your first script is only changing the ANIMATOR boolean value. NOT the actual boolean value. So you'll need to change your stuff to
if(Input.GetKeyDown(KeyCode.P)){
stick = true;
animator.SetBool("stick", stick); //We're setting the bool value to the same as the "stick" variable so it only has to be set one time (more dynamic)
}
And do the same for the other one.
Now your landinggear script should work :)
ALSO at the top of your landinggear script i'd recommend creating a "panel" reference at the top of your script.
So where you have your anim and player add in a
private panel pan;
and in Start()
pan = GameObject.Find("panel").GetComponent<panel>();
and then refer to pan instead of the GameObject.Find blah blah stuff
The reason for this is GameObject.Find("blahblah").GetComponent(); is VERY slow and calling it every frame can slow your game down. Setting up a reference to this "panel" you made is one, easier to write/read, and is also much faster :)
Really appreciate for the answer @corn @SterlingSoftworks, so i have do some editing but still come out with this error:
NullReferenceException: Object reference not set to an instance of an object landinggear.Update () (at Assets/landinggear.cs:26)
script for panel :
public class panel : $$anonymous$$onoBehaviour {
public bool stick;
private Animator animator;
private GameObject player;
// Use this for initialization
void Start () {
animator = GetComponent<Animator> ();
}
// Update is called once per frame
void OnTriggerStay(Collider other) {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.P)) {
animator.SetBool ("stick", true);
stick = true;
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.O)) {
animator.SetBool ("stick", false);
stick = false;
}
}
}
script for landing gear:
public class landinggear : $$anonymous$$onoBehaviour {
private panel pan;
private Animator anim;
private GameObject player;
// Use this for initialization
void Start () {
pan = GameObject.Find ("panel").GetComponent<panel> ();
}
// Update is called once per frame
void Update () {
if(pan.stick)
{
anim.SetBool("retraction",true);
}
if(!pan.stick)
{
anim.SetBool("retraction",false);
}
}
}
p/s : I still very confuse with using code to call upon checking for the panel boolean is it true or false ( meaning to say under landing gear script ; under the void update ; that part i not sure is it a correct way to tell the landing gear check whether panel boolean is true or false..) $$anonymous$$indly please help me with that.. thanksss
Well another thing that @corn pointed out was that you aren't getting the reference of your Animator in your landinggear script. Put anim = GetComponent<Animator>();
in Start() and that should fix that issue :)
oh, really sorry, how can i miss out to register in the landing gear animator... Thanks man, really really appreciate your help :D
Answer by corn · Jan 21, 2016 at 11:22 PM
You just need to reassign stick
whenever you change the animation :
void OnTriggerStay(Collider other)
{
if (Input.GetKeyDown(KeyCode.P))
{
animator.SetBool("stick", true);
stick = true;
}
if (Input.GetKeyDown(KeyCode.O))
{
animator.SetBool("stick", false);
stick = false;
}
}
And in landinggear, don't forget to get a reference for your anim :
void Start()
{
anim = GetComponent<Animator>();
}
That should be enough to get your script working.
Answer by douglas9525 · Jan 22, 2016 at 01:25 PM
Really thanks for the reply @corn @SterlingSoftworks So i have do some editing and still it shows error :
NullReferenceException: Object reference not set to an instance of an object landinggear.Update () (at Assets/landinggear.cs:26)
script for panel:
public class panel : MonoBehaviour {
public bool stick;
private Animator animator;
private GameObject player;
// Use this for initialization
void Start () {
animator = GetComponent<Animator> ();
}
// Update is called once per frame
void OnTriggerStay(Collider other) {
if (Input.GetKeyDown (KeyCode.P)) {
animator.SetBool ("stick", true);
stick = true;
}
if (Input.GetKeyDown (KeyCode.O)) {
animator.SetBool ("stick", false);
stick = false;
}
}
script for land gear:
public class landinggear : MonoBehaviour {
private panel pan;
private Animator anim;
private GameObject player;
// Use this for initialization
void Start () {
pan = GameObject.Find ("panel").GetComponent<panel> ();
}
// Update is called once per frame
void Update () {
if(pan.stick)
{
anim.SetBool("retraction",true);
}
if(!pan.stick)
{
anim.SetBool("retraction",false);
}
}
}
p/s: I still very confuse about using code to call upon checking for the control panel boolean is it true or false ( So saying that under landing gear script ; void update ; that part i not sure is it a correct way to check the boolean.. ) ( really appreciate you guys can help.. thanksss
You forgot to get a reference to anim.
void Start()
{
anim = GetComponent<Animator>();
pan = GameObject.Find ("panel").GetComponent<panel> ();
}
Your Update function is correct, but since anim
was not initialized (it's null), it raised an exception. Just update your start function and this will work.