- Home /
Bool from referenced script not updating?
Hi there,
I've been playing with this for about a week off-and-on, and I can't seem to properly reference a bool I've created in one script to another.
The debug.log text in the first script (NPC_Dialog) appears, and the bools work fine. However, the referenced bool from the second script, while recognized as a referenced bool, does not change or present it's debug.log text to the console.
Thanks for the help! Hope it's something simple, as I've set up many instances in my game to be referenced in such a way.
Addyarb
**///Script 1 (NPC_Dialog)**
void OnGUI () {
GUI.skin = NecroGUI;
GUILayout.BeginArea (new Rect (Screen.width / 2, Screen.height / 2, 400, 400));
if (DisplayDialog && !ActivateQuest) {
GUILayout.Label (Questions [0]);
GUILayout.Label (Questions [1]);
if (GUILayout.Button (answerButtons [0])) {
exclamationPointBobbing = true;
Debug.Log ("Exclamation Bool Set TO true");
toggleGUI = true;
ActivateQuest = true;
hasDoneQuest = false;
DisplayDialog = false;
instructions = true;
}
if (GUILayout.Button (answerButtons [1])) {
DisplayDialog = false;
}
}
if (DisplayDialog && ActivateQuest && hasDoneQuest == true) {
GUILayout.Label (Questions [2]);
if (GUILayout.Button (answerButtons [2])) {
DisplayDialog = false;
exclamationPointBobbing = false;
}
}
GUILayout.EndArea ();
if (ActivateQuest && toggleGUI && instructions == true) {
connectionWindowRect = GUILayout.Window (0, connectionWindowRect, InstructionsWIndow, "Quest: Lost Locket");
}
///Script 2.. "Exclamation Mark Enable"
using UnityEngine;
using System.Collections;
public class QuestPointEnable : MonoBehaviour {
public NPC_Dialog npc;
public Transform exclamationPoint;
// Use this for initialization
void Start () {
exclamationPoint = transform;
npc = GameObject.Find("NPC").GetComponent<NPC_Dialog>();
}
// Update is called once per frame
void Update () {
if (npc.exclamationPointBobbing = true) {
Debug.Log ("Bool Triggered For QuestPoint Enabled");
exclamationPoint.renderer.enabled = true;
}
}
}
in update on script2 you are not checking the variable but assigning it.
Remeber the == operator is for checking the = operator is for chanigng values.
also; do you have more than one npc on the scene?
I do have more than one NPC, yes. Well, I have a rough copy of the script to use as a different NPC dialog. I will check the update right now, thanks!
Also, can you elaborate on which = to change? (what line?) From what you wrote, I'm guessing the first one, but I tried that and it made no difference so far. I tried to change the second one, but I got an error: (20,90): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Update: I've tried referencing the transform of my gameObject "Exclamation point" within the first script itself, but still no luck. I'd be just as happy if I could make the thing enable/disable in response to a bool change, but I'm having no luck at all.
Answer by Kiwasi · Jul 07, 2014 at 11:57 PM
Change line 18 of the second script to the following. Otherwise you will be changing the value instead of checking it. Has caught me out once or twice...
if (npc.exclamationPointBobbing == true) {
One way to avoid this problem is to use properties. You can change your variable declaration to the following, which means that only the one script can change it, but any script can read it.
public bool exclamationPointBobbing {get; private set;}
That edit doesn't seem to solve my issue I'm afraid. I completely see what you mean now about the == vs. =, but the bool is still not being read as true on the second script, because the debug text nor the transform become enabled.
Answer by Addyarb · Jul 08, 2014 at 03:58 AM
The (now obvious) reason it wasn't working is because the transform of the object containing the script that was referencing the bool of the other script was disabled. (I wanted to enable it via a script that was contained within the object). A stupid mistake, but once I enabled the object and switched the transform.enable to mesh renderer.enable, it worked just fine.
Thanks for the help! Hope this saves someone like me a lot of trouble one day.
Your answer
Follow this Question
Related Questions
Reference creator of gameObject 1 Answer
Destroying Prefab Also Destroys Prefab Reference 1 Answer
How to have an instance only reference its parent 1 Answer
How to disable referenced Prefabs being preallocated in memory?? 0 Answers
How to resolve script references in scene bundles from dynamically loaded assemblies? 1 Answer