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 /
avatar image
-1
Question by SentryCG · Feb 10, 2014 at 01:55 AM · function updateanother scriptaccessing scripts

[Closed]Updating changing bool from one script to another.

So essential I need assistance in updating a private bool from a public bool of another script; The following is the first script which has the hitComm bool updating properly when a click is detected

 public bool hitComm;
 public Transform thisTransform;
 
 private bool hitVerify = false; 
 
 void Awake () {
         hitComm = hitVerify;
     }
 
 void Update () {
 
         //Raycaster
         Ray constantRay = Camera.main.ScreenPointToRay(Input.mousePosition);
         Debug.DrawRay(constantRay.origin,constantRay.direction * 100,Color.red);
         
         if (Input.GetMouseButtonDown(0))
         {
             
             RaycastHit2D hit = Physics2D.GetRayIntersection(constantRay,Mathf.Infinity);
             
             if(hit.collider != null && hit.collider.transform == thisTransform)
             {
                 hitVerify = true;
                 hitComm = hitVerify;
 
             } else{
                 hitVerify = false;
                 hitComm = hitVerify;
             }
 
             Debug.Log("hitComm: " + hitComm);
         }
 
         hitComm = hitVerify;
         
     }

This is the second script, the one I am having trouble with attaining the proper updated bool state from the first script hitComm var.

 using UnityEngine;
 using System.Collections;
 
 public class HitTest : MonoBehaviour {
     public CharacterClick greenLight;
 
     private bool hitChecker;
 
     // Use this for initialization
     void Start () {
         renderer.material.color = new Color(1,0,0);
         greenLight = new CharacterClick();
     }
     
     // Update is called once per frame
     void Update () {
 
         hitChecker = greenLight.hitComm;
 
         if(Input.GetMouseButtonDown(0))
         {
             Debug.Log("hitChecker: " + hitChecker);
 
             if(hitChecker)
             {
                 renderer.material.color = new Color(0,1,0);
             } else if (!hitChecker){
                 renderer.material.color = new Color(1,0,0);
             }
 
         }
     }
 }

So the hitChecker here which is supposed to be referencing hitComm from the other script, with every Update, it does not reapply the value state of what hitComm is. I really appreciate some help on this.

Here's a visual: alt text

Comment
Add comment · Show 3
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 FreeTimeDev · Feb 11, 2014 at 06:07 PM 0
Share

http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

avatar image DajBuzi · Feb 11, 2014 at 06:45 PM 0
Share

You're creating a new instance of your CharacterClick script and this new instance isnt c9nnected with any game object so it's not updating. Change this :

 greenLight = new CharacterClick();

To this:

 greenLight = GameObject.Find("name of game object containing your script").GetComponent<CharacterClick>();
avatar image SentryCG · Feb 11, 2014 at 07:04 PM 0
Share

It was not connected to the game object! Thank you!

1 Reply

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

Answer by highpockets · Feb 11, 2014 at 06:47 PM

Is the character clicker script on a game object already?? If so, you don't want the second script to create a new character clicker, but rather reference the game object:

 CharacterClickerScriptName characterClickerScriptt;
 
 void Start()
 {
 
 characterClickerScript = GameObject.Find("Name of  clicker Object").GetComponent<CharacterClickerScript>();
 characterClickerScript.hitChecker = false;
 }
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 SentryCG · Feb 11, 2014 at 07:05 PM 0
Share

Thank you for your answer, indeed it was not connected to the game object to target.

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

21 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

Related Questions

Acceleration script 2 Answers

How do you destroy more than one prefab clone? 1 Answer

How to access public variable in namespace 3 Answers

Script can't find Component within Start() - C# 1 Answer

Call boolean from another script 2 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