Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jaidan · Feb 05, 2015 at 06:04 PM · c#scriptingbasicsbooleanbool

Check bool from another script and use in another script C#

I have a public bool set up in one script which basically checks if the game "isPaused" and i want to accsess this from my player movement script so i can say something like, if the game is paused, then set the velocity back to 0, or something along thoose lines. Thank you

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by KazooCar69 · Feb 05, 2015 at 06:11 PM

You need a reference to the script unless it's static.

For instance, lets say your script is called GameStateManager, and this is the script that handles pausing and has a public bool called isPaused. It's on an empty gameobject somewhere in your scene. We'll say you called the gameobject "Game State Manager." It's instantiated (meaning there's an instance of it that 'exists', it's not just theoretical) so you can reference it.

Inside your player movement script, somewhere you need to store that reference. Usually for something like this you'd say

private GameStateManager m_GameStateManager;

and then in Start

m_GameStateManager = GameObject.Find("Game State Manager").GetComponent();

will work for example. Then when you want to do your check by just using your m_GameStateManager.isPaused that you now have access to. Let me know if you need any further help.

Note that you don't want to be 'finding' something every frame, so using it in start to get your reference ONCE is a much faster implementation. In addition, we're finding the object by it's name, which isn't as safe as, say, finding it by it's tag.

Comment
Add comment · Show 3 · 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 Jaidan · Feb 05, 2015 at 07:11 PM 0
Share

I tried this but i got an error message:

Assets/Scripts/$$anonymous$$ovement.cs(20,36): error CS0120: An object reference is required to access non-static member `Pause$$anonymous$$enuScript.isPaused'

Here is my code:

using UnityEngine; using System.Collections;

public class $$anonymous$$ovement : $$anonymous$$onoBehaviour {

private Pause$$anonymous$$enuScript pause$$anonymous$$enuScript;

public float movementSpeed;

public int jumpHeight; float jumpSpeed; Vector3 velocity;

void Start(){

  pause$$anonymous$$enuScript = GameObject.Find ("$$anonymous$$anager").GetComponent<Pause$$anonymous$$enuScript> ();
 
  jumpSpeed = $$anonymous$$athf.Sqrt(-2*Physics.gravity.y*jumpHeight) + 0.1f;
 
  if(Pause$$anonymous$$enuScript.isPaused)
      rigidbody.velocity = Vector3.zero;

}

void OnCollisionEnter(Collision collision) {

  foreach (ContactPoint contact in collision.contacts) {
      Application.LoadLevel ("SceneOne");
  }

}

void Update(){

  rigidbody.AddRelativeForce(Vector3.right * 10 - rigidbody.velocity);
 
  if(Input.Get$$anonymous$$eyDown("up") || (Input.Get$$anonymous$$ouseButtonDown(0)))
  {
      velocity = rigidbody.velocity;
      velocity.y = jumpSpeed;
      rigidbody.velocity = velocity;
  }

} }

avatar image KazooCar69 · Feb 05, 2015 at 07:21 PM 0
Share

Pause$$anonymous$$enuScript.isPaused' is what you're referencing, pause$$anonymous$$enuScript.isPaused' is what you should be. Notice you tried to call the CLASS ITSELF, not the reference to it. That's why in my example I put m_ before it, so that I don't get the two confused.

if(Pause$$anonymous$$enuScript.isPaused)

is where the error is, change it to

if(pause$$anonymous$$enuScript.isPaused)

avatar image Jaidan · Feb 05, 2015 at 07:36 PM 0
Share

Ahh okay thank you, i will remember the tip to have the m_ before, thank you mate :D

avatar image
1

Answer by Mmmpies · Feb 05, 2015 at 06:11 PM

Pretty regular question. Say your menu script is on an empty gameObject called menuObject and the scripts called menuScript do this:

 private menuScript myMenuScript;
 
 void Start(){
 
     myMenuScript = GameObject.Find("menuObject").GetComponent<menuScript>();
 
 }
 
 // then you can test the bool
 
 if(myMenuObject.myBool)
 // do code

That assumes your bool is called myBool.

Of course you could just set Time.timeScale = 0 when in your menu and Time.timeScale = 1 when you exit the menu. It'll freeze the rest of the game butt the UI will still interact.

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 Jaidan · Feb 05, 2015 at 06:43 PM 0
Share

I tried this but i got an error message:

Assets/Scripts/$$anonymous$$ovement.cs(20,36): error CS0120: An object reference is required to access non-static member `Pause$$anonymous$$enuScript.isPaused'

Here is my code:

using UnityEngine; using System.Collections;

public class $$anonymous$$ovement : $$anonymous$$onoBehaviour {

 private Pause$$anonymous$$enuScript pause$$anonymous$$enuScript;

 public float movementSpeed;

 public int jumpHeight;
 float jumpSpeed;
 Vector3 velocity;
 
 void Start(){

     pause$$anonymous$$enuScript = GameObject.Find ("$$anonymous$$anager").GetComponent<Pause$$anonymous$$enuScript> ();

     jumpSpeed = $$anonymous$$athf.Sqrt(-2*Physics.gravity.y*jumpHeight) + 0.1f;

     if(Pause$$anonymous$$enuScript.isPaused)
         rigidbody.velocity = Vector3.zero;
 }

 void OnCollisionEnter(Collision collision) {

     foreach (ContactPoint contact in collision.contacts) {
         Application.LoadLevel ("SceneOne");
     }
 }


 void Update(){

     rigidbody.AddRelativeForce(Vector3.right * 10 - rigidbody.velocity);

     if(Input.Get$$anonymous$$eyDown("up") || (Input.Get$$anonymous$$ouseButtonDown(0)))
     {
         velocity = rigidbody.velocity;
         velocity.y = jumpSpeed;
         rigidbody.velocity = velocity;
     }
 }

}

avatar image Paximillian · Aug 16, 2015 at 11:31 PM 0
Share

On line 15 you're using the type name, not the instance(Notice the capital P?). You need to access the instance you have created(Small p in the beginning), that should fix it.

avatar image
0

Answer by Magikarp_FTW · Aug 16, 2015 at 10:36 PM

http://answers.unity3d.com/questions/418609/how-do-i-check-if-bool-is-true-from-another-script.html

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

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

22 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

Related Questions

OnTriggerStay not working correctly. 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to Set Application.isFocused? 1 Answer

Get Boolean from another Script, C# 2 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