- Home /
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
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.
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;
}
} }
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)
Ahh okay thank you, i will remember the tip to have the m_ before, thank you mate :D
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.
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;
}
}
}
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.
Your answer
Follow this Question
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