Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by douglas9525 · Jan 21, 2016 at 10:10 PM · c#unity 5unityeditormonodevelop

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image douglas9525 · Jan 22, 2016 at 01:26 PM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 :)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image douglas9525 · Jan 22, 2016 at 02:50 AM 0
Share

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

avatar image SterlingSoftworks douglas9525 · Jan 22, 2016 at 05:09 AM 0
Share

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 :)

avatar image douglas9525 SterlingSoftworks · Jan 22, 2016 at 06:43 AM 0
Share

oh, really sorry, how can i miss out to register in the landing gear animator... Thanks man, really really appreciate your help :D

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image corn · Jan 22, 2016 at 01:41 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity3d and monodevelop c# scripting on ubuntu 16.04 IntelliSense problems 0 Answers

Draw a line from object to indicate power 0 Answers

Error MSB3645: .NET Framework v3.5 Service Pack 1 was not found. In order to target ".NETFramework,Version=v3.5", .NET Framework v3.5 Service Pack 1 or later must be installed. (MSB3645) (Assembly-CSharp-firstpass) 2 Answers

Tesseract for Unity 0 Answers

Assets/_Scripts/Md5.cs(1,15): error CS0116: A namespace can only contain types and namespace declarations 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges