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 TysonG · Mar 24, 2019 at 04:41 PM · script.booleanreferencebool

Checking a bool's state from another script?

Hey everyone, so I'm not too familiar with referencing/interacting with parts of code between scripts, so I don't know if I'm doing this right. I have a ladder that is a collider set as a trigger and a player (with a PlayerController script) that Raycasts to check whether they are within the ladder or not. If they are on the ladder, isClimbing is set to true and they can climb the ladder. At the top of the ladder, I have an empty GameObject called LadderTop which has a platform effector on it, with a LadderTop script attached to it. In the LadderTop script (below), I change the direction the platform effector lets you travel through it whether you are climbing up or down (up arrow or down arrow). I need to check in this script whether isClimbing is true or not because if not, the platform effector will rotate whenever I hit the up or down arrow even if I'm not in the ladder's collider. With this script though, I get the error "NullReferenceException: Object reference not set to an instance of an object LadderTop.Update () (at Assets/__Scripts/LadderTop.cs:19)" at each of the lines where I try to reference isClimbing. Also, in the PlayerController script, isClimbing is a public bool. Any help is appreciated.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LadderTop : MonoBehaviour {
 
     private MegaManController playerController;
     private PlatformEffector2D ladderTop;
 
     void Start () {
 
         ladderTop = GetComponent<PlatformEffector2D>();
         playerController = GetComponent<MegaManController>();
  
     }
     
     void Update () {
         
         if (Input.GetKey(KeyCode.DownArrow) && playerController.isClimbing == true)
         {
             ladderTop.rotationalOffset = 180;
         }
 
         if (Input.GetKey(KeyCode.UpArrow) && playerController.isClimbing == true)
         {
             ladderTop.rotationalOffset = 0;
         }
 
     }
 }
 



Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by WarmedxMints · Mar 24, 2019 at 04:47 PM

It sounds like the MagaManController class is not attached to the same gameobject as the LadderTop class. GetComponent looks for objects on the same gameObject. GetComponentInChlidren looks for an object on the gameObject and its children. FindObjectOfType looks for objects in the hierarchy

It sounds like you should be using FindObjectOfType

 using UnityEngine;
 
 public class LadderTop : MonoBehaviour
 {
 
     private MegaManController playerController;
     private PlatformEffector2D ladderTop;
 
     private void Start()
     {
 
         ladderTop = GetComponent<PlatformEffector2D>();
         playerController = FindObjectOfType<MegaManController>();
 
     }
 
     private void Update()
     {
         if (!playerController.isClimbing)
             return;
 
         if (Input.GetKey(KeyCode.DownArrow))
         {
             ladderTop.rotationalOffset = 180;
         }
 
         if (Input.GetKey(KeyCode.UpArrow))
         {
             ladderTop.rotationalOffset = 0;
         }
 
     }
 }
Comment
Add comment · Show 2 · 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 TysonG · Mar 24, 2019 at 05:34 PM 0
Share

Thanks! It works fine with no errors now, appreciate it! Have a good one.

avatar image WarmedxMints TysonG · Mar 24, 2019 at 05:51 PM 0
Share

Your welcome. Can you mark the question as answered please so others know the issue has been solved.

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

111 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

reset bool on timer passed by ref 1 Answer

GetComponent, set boolean to true but it willn't revert 3 Answers

How to change function of a key with a boolean 1 Answer

I'm having trouble setting a bool. 2 Answers

One Script Referred by Another Script used by Multiple Objects 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