- Home /
The question is answered, right answer was accepted
how do i change a bool from another script
i am trying to make it so if the object is collided with the another object enough times a bool from another script will activate. but the following error occurs with the enemydestroyed.enedeath: "an object reference is required for the nonstatic field method or property"
My script
using UnityEngine;
using System.Collections;
public class swordCollideWithEnemybasic : MonoBehaviour {
public int enehealth = 5;
public int knockback = 1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnCollisionEnter (Collision hit) {
if (enehealth > 0)
if (hit.gameObject.name == "sword");
gameObject.renderer.material.color = Color.red;
StartCoroutine (turngreyDelayed ());
if (enehealth <= 0)
enemydestroyed.enedeath = true;
}
IEnumerator turngreyDelayed() {
yield return new WaitForSeconds(0.2f);
gameObject.renderer.material.color = Color.grey;
int addDamage = enehealth;
int newDamage = enehealth - 1;
enehealth = newDamage;
Debug.Log(enehealth);
transform.Translate (0,0,knockback * Time.deltaTime);
}
}
another script:
using UnityEngine;
using System.Collections;
public class enemydestroyed : MonoBehaviour {
public bool enedeath = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter (Collision hit) {
if (enedeath = true);
if (hit.gameObject.name == "sword")
gameObject.renderer.material.color = Color.red;
StartCoroutine (turngreyDelayedDestroy());
}
IEnumerator turngreyDelayedDestroy() {
yield return new WaitForSeconds(0.2f);
gameObject.renderer.material.color = Color.white;
yield return new WaitForSeconds(0.1f);
Destroy (gameObject);
//^insert animation"//
}
}
That's because your first script has no idea what enemydestroyed is....
you will need to get the gameobject with the script on and then use:
enemydestroyed ed = GameObject.FindObjectWithTag("tagused").getComponent(); ed. enedeath = true;
however, this is using a tag that is assigned to the gameobject trying to get. There are other techniques, but this is a popular one.
Answer by Ben-Stoneman · Jul 15, 2014 at 11:00 AM
Here is an example of how to access variables via another script:
I have made two objects and one has the tag "BoolKeeper". Here is the script attached to "BoolKeeper":
using UnityEngine;
using System.Collections;
public class IGotBools : MonoBehaviour
{
public bool isOnFire;
public bool isElectrifying;
}
The other object has this script attached:
using UnityEngine;
using System.Collections;
public class BoolChanger : MonoBehaviour
{
public IGotBools boolBoy;
void Start()
{
// Finds the object the script "IGotBools" is attached to and assigns it to the gameobject called g.
GameObject g = GameObject.FindGameObjectWithTag (BoolKeeper);
//assigns the script component "IGotBools" to the public variable of type "IGotBools" names boolBoy.
boolBoy = g.GetComponent<IGotBools> ();
// accesses the bool named "isOnFire" and changed it's value.
boolBoy.isOnFire = false;
}
}
In the above example, as the variable "boolBoy" is public, we can make the relationship in the inspector by dragging the GameObject with the IGotBools instance onto the "booolBoy" slot.
@BenStoneman Thanks for this example! I have been searching around for an example of changing a bool in another script and many of the examples i found either didn't work correctly or weren't what i needed but this example worked perfectly thanks.
Thanks for the script help. I tried a few other topics and this one helped the best. I couldn't use the "findObjectWithTag" bit, but I was able to get a public reference through the inspector.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Changing a bool on only 1 instance of a prefab instead of all? 2 Answers
One variable set TRUE when another must be false. TOGGLE 3 Answers
How do I disable one button out of multiple buttons without spamming declaration? 1 Answer