- Home /
How to access the value of a bool in another script c#?
script 1:
public bool Enabled;
if (Input.GetKeyUp(KeyCode.Escape)) {
Enabled = !Enabled;
}
script 2:
if (!Enabled) {
debug.log ("ESC has been pressed");
}
My problem is that the second script doesn't know what Enabled is. This is a very simplified version of my code but if this can be answered ill be able to implement it in my actual code. So basically what i want to do is have two different scrips but when Enabled changes to true or false i want it to also change the other script. Please answer the question if possible without the solution being put it all in one script.
This is what ive got but i get the error - NullReferenceException: Object reference not set to an instance of an object snipershooting.Update () (at Assets/Standard Assets & $$anonymous$$ine/$$anonymous$$y Scripts/snipershooting.cs:23)
snipershooting is the script that controls how the sniper shoot.
using UnityEngine; using System.Collections; public class snipershooting : $$anonymous$$onoBehaviour { public bool GunEnabled; public GameObject Bullet; public GameObject muzzleflare; public GameObject shootingdirection; public Vector3 bulletoffset; Back$$anonymous$$enuScript Script1Object; void start () { Script1Object = GetComponent<Back$$anonymous$$enuScript>(); } // Update is called once per frame void Update () { if (Input.Get$$anonymous$$ouseButtonDown(0) && !Script1Object.Enabled) { bulletoffset = transform.position + (transform.up * 0f ) + ( transform.forward * 0f)+ ( transform.right * -0f ); Instantiate(Bullet, bulletoffset, shootingdirection.transform.rotation); Instantiate(muzzleflare , bulletoffset, shootingdirection.transform.rotation); } } }
this error only comes up when i put && !Script1Object.Enabled into the if statment.
Assets/Standard Assets & $$anonymous$$ine/$$anonymous$$y Scripts/snipershooting.cs(23,58): error CS0176: Static member `Back$$anonymous$$enuScript.Enabled' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d i think if we fix this error things could never be better. i think this is the problem causing the other one.
Answer by united4life · Nov 19, 2013 at 05:32 AM
First you have to access script1 in script2
script2
---------------------
Script1 script1Object;
void Start()
{
script1Object = GetComponent<Script1>();
}
void Example()
{
if(!script1Object.Enabled) {
debug.log ("ESC has been pressed");
}
}
Answer by supernat · Nov 19, 2013 at 05:33 AM
There are several ways to do it. If script 1 will only ever exist on one object, make Enabled static. Then from script 2, you can use "Script1.Enabled".
Otherwise, you would need a reference to script 1. I.e. Script1 obj = GetScript1SomeHow(); Then use obj.Enabled
hey i have the error - Assets/Standard Assets & $$anonymous$$ine/$$anonymous$$y Scripts/snipershooting.cs(26,58): error CS0176: Static member `Back$$anonymous$$enuScript.Enabled' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d. i think i accidentally asked for a solution to this error through an answer on this lel.
Static members are accessed via class while instances members are accessed via instance. You are trying to access a static member by an instance, so remove the keyword static or use the class name to access it ins$$anonymous$$d of a variable