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 /
This question was closed Nov 16, 2015 at 05:39 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
3
Question by dylan760 · Jul 15, 2014 at 10:24 AM · c#boolaccessing from any script

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"//
     }
 }



Comment
Add comment · Show 2
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 tanoshimi · Jul 15, 2014 at 10:25 AM 1
Share

That's because your first script has no idea what enemydestroyed is....

avatar image Klarax · Jul 15, 2014 at 10:40 AM 0
Share

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.

1 Reply

  • Sort: 
avatar image
10
Best Answer

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;
 
     }
 
 }

Comment
Add comment · Show 4 · 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 Adam-Buckner ♦♦ · Jul 15, 2014 at 12:29 PM 1
Share

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.

avatar image gocraig · Nov 16, 2015 at 05:38 PM 0
Share

Thanks @BenStoneman ... that one worked for me.

avatar image kaikhan · Jun 21, 2016 at 03:36 PM 0
Share

@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.

avatar image Chasbas_80 · Dec 24, 2016 at 04:58 PM 0
Share

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

Answers Answers and Comments

9 People are following this question.

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

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


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