• 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
Question by fabian_rensch · Dec 17, 2015 at 09:56 PM · c#renderingbooleanreferencebool

Referenced Bool is not updating

I've been trying to fix this for some hours but it just won't work out.

I have two scripts. One that is attached to the player (Script1) and one that is attached to a prefab (Script2).

In Script1 I want to use a referenced bool from Script2. That bool is true when the mouse is over the prefab:

 void OnMouseOver()
     {
         isOver = true;
     }

The bool gets false when not hovering the prefab. Until here everything works. My reference in the other script looks like this:

 public bool over;
 public gameObject boxPrefab; // Referenced in the Inspector
 
 void Update ()
     {
         over = boxPrefab.GetComponent<DestroyBox>().isOver;
         }
 

When I now try to check with Debug.Log if over is true it won't give me any response in the console.

What is my fault? I'm sure it's something super stupid.

Comment

People who like this

0 Show 6
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 commodore · Dec 17, 2015 at 10:22 PM 0
Share

Is Script2 attached to a GameObject?

avatar image fabian_rensch commodore · Dec 17, 2015 at 10:24 PM 0
Share

It is attached to the Prefab (boxPrefab).

avatar image commodore fabian_rensch · Dec 17, 2015 at 10:32 PM 0
Share

Ok so script1 is the one starting with public bool over;?

Are you doing Debug.Log(over) right after over = boxPrefab.GetComponent<DestroyBox>().isOver;?

If nothing is showing up then it's probably because script1 isn't attached to anything in the scene

Show more comments
avatar image commodore · Dec 17, 2015 at 10:42 PM 0
Share

Think you could post the full scripts for script1 and script2?

avatar image fabian_rensch commodore · Dec 17, 2015 at 10:50 PM 0
Share

Posted another comment with both scripts.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by fabian_rensch · Dec 17, 2015 at 10:49 PM

Both Scripts here.

Script 1 is for instantiating and counting the prefabs. Script 2 is for Destroying the clicked prefab.

Script BoxMagic aka Script1

using UnityEngine; using System.Collections; using UnityEngine.UI;

 public class BoxMagic : MonoBehaviour {
 
     public GameObject boxPrefab;
     public Vector3 pz;
     public int boxAnzahl = 5;
     public bool isEmpty = false;
     public bool isFull = true;
     public Text countText;
     public bool over;
 
 
 
 
 
     void Start ()
     {
 
     }
     
     void Update ()
     {
         over = boxPrefab.GetComponent<DestroyBox>().isOver;
         pz = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         pz.z = 0;
 
         setBox ();
         deleteBox ();
 
         if (boxAnzahl >= 5) {
             isFull = true;
         } else {
             isFull =false;}
         if (boxAnzahl <= 0) {
             isEmpty = true;
         } else {
             isEmpty = false;
         }
 
         if (over == true) 
         {
             Debug.Log("over!");
         }
     
 
 
     }
     void SetCountText()
     {
         countText.text ="Boxes: "+ boxAnzahl.ToString()+"/5";
     }
 
     void setBox()
     {
         if (isEmpty == false )
             {
             if (Input.GetMouseButtonDown (1)) 
                 {
                 boxAnzahl--;
                 SetCountText();
                 Instantiate(boxPrefab, pz, Quaternion.Euler(0, 180, 0));
                 }
             }
         else 
         {
             isFull =false;
         }
         
 
 
     }
 
     void deleteBox()
     {
         if (isFull == false && over == true) {
             if (Input.GetMouseButtonDown (2)) {
                 boxAnzahl++;
                 SetCountText ();
             }
         } else {
             isEmpty =false;
         }
         
 
     }
 
 
 
 
 
 }


Script DestroyBox aka Script 2

 using UnityEngine;
 using System.Collections;
 
 public class DestroyBox : MonoBehaviour {
 public bool isOver;
 
 
 
 
 
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
     
     
     }
     void OnMouseOver()
     {
         isOver = true;
         if(Input.GetMouseButtonDown(2))
               {
             Destroy(this.gameObject);
             }
 
         }
     void OnMouseExit()
     {
 
         isOver = false;
     }
 }
 


Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

43 People are following this question.

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

Related Questions

List of Structs variable values not changing 1 Answer

check bool across a script 1 Answer

Why is my bool being set to true when program is run? 1 Answer

My Bool function is returning False but it should be true in my opinion. Csharp C# 0 Answers

seting a bool false/true,Bool which isnt supposed to be active is in the begining 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