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 /
avatar image
0
Question by matthew_gigante · Oct 01, 2017 at 06:02 PM · getcomponentbooleanboolfindgameobjectwithtag

Trouble setting a bool value between two objects

I am trying to make a quiz style sound match game, where a sound is played, and the player must choose between two balls which play similar sounds. One is of the same pitch and one is slightly different. I am trying to set a bool between the right answer(true) and wrong answer(false) but cannot understand the answers I have read about getComponent and using it to change the bool.

Here is my code for the ball1 (the script with the bool in it, and also the correct answer:

  using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ball1Click : MonoBehaviour {
     public bool isCorrect;
     public AudioSource beep;
     private ball2Click truthScript; 
 
     // Use this for initialization
     void Start () {
         isCorrect = false;
     }
     
     // Update is called once per frame
     void Update () {
 
     }
     void OnMouseDown(){
         beep.Play();
         isCorrect = true;
     }
 }
 

Here is the code for the other ball, the wrong answer that is supposed to get the component and change it to false if clicked on:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ball2Click : MonoBehaviour {
 
     public AudioSource beep;
     public ball1Click boolTruth;
 
 
     void Start () {
         //GameObject ball1 = GameObject.FindGameObjectWithTag (ball1);
         //boolTruth = ball1.GetComponent<ball1Click> ();
     }
 
 
     void Update () {
 
     }
     void OnMouseDown(){
         ballTruth.GetComponent (ball1Click).OnMouseDown (false); 
         beep.Play();
         boolTruth.isCorrect = false;
     }
 }


Comment
Add comment · Show 1
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 tmalhassan · Oct 01, 2017 at 06:19 PM 0
Share

so you're trying to access the bool from the other script using this code: boolTruth.isCorrect = false;?

1 Reply

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

Answer by tmalhassan · Oct 01, 2017 at 06:55 PM

Hello @mgiga0420.

I haven't checked the logic of your code. But from what I see, you're trying to access the bool in an incorrect way. To access that bool from another script, your code should look like this:

Ball1Click:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class Ball1Click : MonoBehaviour
 {
     public static Ball1Click Instance {set; get;}

     public bool isCorrect;
     public AudioSource beep;

     void Awake()
     {
         Instance = this;
     }

     // Use this for initialization
     void Start()
     {
         isCorrect = false;
     }

     void OnMouseDown()
     {
         if (isCorrect == false)
         {
             beep.Play();
             isCorrect = true;
         }
     }
 }

Ball2Click:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class Ball2Click : MonoBehaviour {

   public AudioSource beep;

     void Start()
     {
         //GameObject ball1 = GameObject.FindGameObjectWithTag (ball1);
         //boolTruth = ball1.GetComponent<ball1Click> ();
     }

     void OnMouseDown()
     {
         if (Ball1Click.Instance.isCorrect == true)
         {
             beep.Play();
             Ball1Click.Instance.isCorrect = false;
         }
     }
 }

  • Also take note that the name of the class always starts with an upper case letter.

  • I removed the reference of the second script in the first one because I found no use for it. you can always add it back the same way I referenced the first script.

Keep in mind that I made a lot of assumptions to what you want, but you can always reply back and explain more so people could assist you furthermore.

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 matthew_gigante · Oct 02, 2017 at 01:15 PM 0
Share

This code did work for me, but for reference the way I fixed my code before reading this is as follows:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ball2Click : $$anonymous$$onoBehaviour {
 
     public AudioSource beep;
     public ball1Click boolTruth;
 
     void On$$anonymous$$ouseDown(){
         beep.Play();
         boolTruth.isCorrect = false;
     }
 }


The public variable receives my ball1 script, and isCorrect's value is changed on click of the object in this script, then an answer button checks the correct value of ball1's script, and either passes or fails you.

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

70 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 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

GetComponent, set boolean to true but it willn't revert 3 Answers

Why is this bool auto turning off? 0 Answers

my button dosent work, became a light switch instead of a normal button (like a bell) 1 Answer

Return a boolean from a function 2 Answers

check if the bool is true from other script c# 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