Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by MIkeHewson86 · Oct 06, 2016 at 02:18 PM · boolean

how do i change a bool from a script in a different game object c#

i am making a 2d platform game in the game there is a player a key and chest

i have labelled the key with the tag "chestKey"

i have a script on the chest called "ChestKey" and a script on the player called player interaction.

in the chestKey script i have a public bool called GotKey

i want to change that bool to true from the player interaction script using ontriggerenter2d

 public class ChestKey : MonoBehaviour {
 
     public LevelManager LevelManager;
     public int chestPoints = 250;
 
    public bool GotKey ;
     
 
 
 
     // Use this for initialization
     void Start ()
     {
         GameObject.FindGameObjectsWithTag("Player");
         GameObject.FindGameObjectsWithTag("chestKey");
         GotKey = false;
     }
 
 
     void OnTriggerEnter2D(Collider2D other)
     {
 
        
 
         if (other.tag == "Player" && GotKey)
         {
             Debug.Log("congratulations chest is open", gameObject);
             LevelManager.AddPoints(chestPoints);    
         }
 
         if (other.tag == "Player" && !GotKey)
         {
             Debug.Log("You Need the Key", gameObject);
         }
 
     }
     
 
 
 
 
     
 }
 

player interaction Script

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 
 public class PlayerInteraction : MonoBehaviour
 {
 
     
     public LevelManager LevelManager;
     public HealthBar HealthBar;
 
     public int cogPoints = 10;
     public int gemPoints = 52;
 
     public GameObject Chest;
    public ChestKey chestKey;
 
     
     public Rigidbody2D rb;
 
     //reference to healthbar script so this script can find it
     void Start()
     {
        GameObject.FindGameObjectsWithTag("HealthBar");
 
         rb = GetComponent<Rigidbody2D>();
         chestKey = GameObject.Find("Chest").GetComponent<ChestKey>();
     }
 
  
 void OnTriggerEnter2D(Collider2D other)
     {
 
 
         Vector2 dir = transform.position - other.transform.position;
         dir.Normalize();
 
         if (other.tag == "chestKey")
         {
             Debug.Log("you have the key", gameObject);
           //  Chest.GetComponent<>;
         }
 
 
 
         //add cog points to score
         if (other.tag == "Cog")
         {
            
             LevelManager.AddPoints(cogPoints);
         }
 
 
         //add gem points to score
         if (other.tag == "Gem")
         {
            
             LevelManager.AddPoints(gemPoints);
         }
 
       
 
         //Health pick up add 40 to health
         if (other.tag == "Health")
         {
             HealthBar.curHealth += 40;
             
         }
 
         // NME takes health of player
         if (other.tag == "NME")
         {
             HealthBar.curHealth -= 10;
             //makes player bounce if hit by enemy
             Rigidbody2D rbOther = this.gameObject.GetComponent<Rigidbody2D>();
             rbOther.AddForce(dir * 1500 );// *Time.deltaTime);
            // Debug.Log(rbOther);
         }
         // if current health = 0 load gameover screen
         if (HealthBar.curHealth == 0)
         {
             SceneManager.LoadScene(1);
 
         }
     }
 }
 

i cant seem to get the reference right can anyone help?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by JScotty · Oct 06, 2016 at 02:26 PM

Hi Mike,

First of all, please start your variable names with lowercase, only use uppercase for properties and functions.

Here how to set an boolean

 void OnTriggerEnter2D(Collider2D other) {
  
          if (other.tag == "chestKey") {
              Debug.Log("you have the key", gameObject);
              chest.gotKey = true;
              // Destroy(other.gameObject); // to detroy the key
          }
 }


I also recommend to check https://unity3d.com/learn/tutorials/topics/scripting/delegates so you can cast an event when you hit the key.

Comment
Add comment · Show 1 · 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 MIkeHewson86 · Oct 07, 2016 at 11:54 AM 0
Share

thanks that really helped

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I check if bool is true from another script? 2 Answers

MoveTowards doesn't work correctly 0 Answers

How can I check a Linecast across multiple layers, and assign the result to a single bool? 1 Answer

Linecast not working after Instantiation of a particular object 0 Answers

Vector3.Distance keeps growing 0 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