- Home /
Bool script error
im having alittle trouble with this script ive typed up. So what its suppose to do is that when you press equiped items button if the current target is mage its suppose to set mage to true. And then if mage is true its suppose to activate the MageEquipedItems script. But heres the problem. First When i target the Mage it doesnt set mage to true, and then the second problem is no matter whats selected if i click equiped items its forcefully sets mage to true even if warrior is selected. Could you help me find out what i did wrong thats in advance!!
using UnityEngine;
using System.Collections;
public class LowerBar1 : MonoBehaviour {
bool button1Clicked = false;
bool button2Clicked = false;
bool button3Clicked = false;
bool button4Clicked = false;
public GameObject target;
public GameObject Mage;
public GameObject Warrior;
public bool mage = false;
// Use this for initialization
void Start () {
mage = false;
}
// Update is called once per frame
void Update () {
if(mage == true){
GetComponent<MageEquipedItems>().enabled = true;
}
}
void OnGUI(){
GUI.Box(new Rect(0,400,200,200), "");
GUI.Box(new Rect(50,405,100,25), "GAME");
GUI.Box(new Rect(0,430,100,25), "Game Type:");
GUI.Box(new Rect(0,455,100,25), "Phase:");
GUI.Box(new Rect(0,480,100,25), "Ally Heroes:");
GUI.Box(new Rect(0,505,100,25), "Enemy Heroes:");
GUI.Box(new Rect(0,530,100,25), "Monsters:");
GUI.Box(new Rect(0,555,100,25), "Total Gold:");
GUI.Box(new Rect(200,400,200,90), "");
GUI.Box(new Rect(200,405,100,20), "Hero:");
GUI.Box(new Rect(200,425,100,20), "Level:");
GUI.Box(new Rect(200,445,100,20), "Health:");
GUI.Box(new Rect(200,465,100,20), "Mana:");
GUI.Box(new Rect(200,490,200,110), "");
GUI.Box(new Rect(400,420,400,180), "");
if(button1Clicked)
GUI.color = Color.red;
else
GUI.color = Color.white;
if (GUI.Button(new Rect(400,400,100,20), "Inventory")){{
button1Clicked = true;
button2Clicked = false;
button3Clicked = false;
button4Clicked = false;
}
GetComponent<Inventory>().enabled = true;
GetComponent<MageEquipedItems>().enabled = false;
}
if(button2Clicked)
GUI.color = Color.red;
else
GUI.color = Color.white;
if (GUI.Button(new Rect(500,400,100,20), "Equiped Items")){{
button1Clicked = false;
button2Clicked = true;
button3Clicked = false;
button4Clicked = false;
}
GetComponent<Inventory>().enabled = false;
mage = GameObject.Find("Mage");
if(target == mage){
mage = true;
}
}
if(button3Clicked)
GUI.color = Color.red;
else
GUI.color = Color.white;
if (GUI.Button(new Rect(600,400,100,20), "Stats")){{
button1Clicked = false;
button2Clicked = false;
button3Clicked = true;
button4Clicked = false;
}
GetComponent<Inventory>().enabled = false;
GetComponent<MageEquipedItems>().enabled = false;
}
if(button4Clicked)
GUI.color = Color.red;
else
GUI.color = Color.white;
if (GUI.Button(new Rect(700,400,100,20), "?")){{
button1Clicked = false;
button2Clicked = false;
button3Clicked = false;
button4Clicked = true;
}
GetComponent<Inventory>().enabled = false;
GetComponent<MageEquipedItems>().enabled = false;
}
}
}
Answer by iwaldrop · Jan 16, 2013 at 10:44 PM
Do you assign 'target' in the inspector? If you don't then your test for 'target == mage' will always be false.
Also, instead of setting the Component to true every frame, you should probably just do it once; when needed.
GetComponent<MageEquipedItems>().enabled = target == mage;
Thanks for responding! The target gets assigned when my targeting script targets it. It shoots a raycast and when it hits a object with the "Hero" tag that object gets selected. Currently there are 4 different objects that have the hero tag. So thats y im trying to figure out how to tell the script if this certain script was selected then hence the "target == mage" but i that might be the wrong code lol. But its setting my component to true every frame how?
Ok. So you're going to want to get the Hero Component (or whatever is special about heros) and query it as to whether it's a mage or not. It might look something like the following:
YourComponent c = hitInfo.collider.gameObject.GetComponent<YourComponent>();
if (c.type == mage)
{
// do something magelike
}
Hero h = hitInfo.collider.gameObject.GetComponent();
if (h.type == mage)
{
GetComponent().enabled = true;
}
if that right?
Very nearly. You need to specify the type with GetComponent.
Hero h = hitInfo.collider.gameObject.GetComponent<Hero>();
if (h is Hero)
GetComponent<$$anonymous$$ageEquippedItems>().enabled = h.type == mage;
Of course 'h.type' is a generic reference to whatever in the Hero class defines it as a mage ins$$anonymous$$d of another type of Hero. So check whatever you use to set it to a mage and you should be good.
Also, maybe you could accept this answer if it works for you, or else it will forever be flagged as unanswered! Thanks!
So should i add more tags in my targeting script so that it targets more than just things with the Hero tag? So it can see what type of hero it is?