- Home /
[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:
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>();
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;
}
Thank you for your answer, indeed it was not connected to the game object to target.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133038im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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