- Home /
Get Component Error
Hello so the problem is Unity isn't reading the code within the second bracket for the exception of the first one. So everything including GetComponent().enabled = false; and down isn't getting executed. What the code is suppose to do is disable all the scipts within that bracket except for the inventory script which it will activate. This is so it only shows the inventory screen and not any of the other screens. When i go to test it, the inventory scripts activates but all the scripts that are suppose to become disabled stay activated. The area in the code in which im having the problem is towards the bottom it'll have *** showing that, that is the area where the script isn't working.
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 bool mage = false;
public bool warrior = false;
public bool archer = false;
public bool rouge = false;
public int currentValue = 0;
public Transform selectedTarget;
public Transform selectedTarget1;
public Transform selectedTarget2;
public Transform selectedTarget3;
void Start ()
{
mage = false;
warrior = false;
archer = false;
rouge = false;
}
void Update ()
{
AdjustCurrentValue(0);
if (Input.GetMouseButtonUp(1))
{
RaycastHit hit; // cast a ray from mouse pointer:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag("Mage"))
{
DeselectTarget(); // deselect previous target (if any)...
selectedTarget = hit.transform; // set the new one...
SelectTarget(); // and select it
}
if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag("Warrior"))
{
DeselectTarget1();
selectedTarget1 = hit.transform;
SelectTarget1();
}
if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag("Archer"))
{
DeselectTarget2();
selectedTarget2 = hit.transform;
SelectTarget2();
}
if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag("Rouge"))
{
DeselectTarget3();
selectedTarget3 = hit.transform;
SelectTarget3();
}
}
}
private void SelectTarget()
{
mage = true;
warrior = false;
archer = false;
rouge = false;
GameObject.Find("Archer").GetComponent<ArcherHealth>().enabled = false;
GameObject.Find("Archer").GetComponent<ArcherTargeting>().enabled = false;
GameObject.Find("Warrior").GetComponent<WarriorHealth>().enabled = false;
GameObject.Find("Warrior").GetComponent<WarriorTargeting>().enabled = false;
GameObject.Find("Mage").GetComponent<MageHealth>().enabled = true;
GameObject.Find("Mage").GetComponent<MageTargeting>().enabled = true;
GameObject.Find("Rouge").GetComponent<RougeHealth>().enabled = false;
GameObject.Find("Rouge").GetComponent<RougeTargeting>().enabled = false;
}
private void DeselectTarget()
{
if (selectedTarget)
{
selectedTarget = null;
}
}
private void SelectTarget1()
{
mage = false;
warrior = true;
archer = false;
rouge = false;
GameObject.Find("Archer").GetComponent<ArcherHealth>().enabled = false;
GameObject.Find("Archer").GetComponent<ArcherTargeting>().enabled = false;
GameObject.Find("Warrior").GetComponent<WarriorHealth>().enabled = true;
GameObject.Find("Warrior").GetComponent<WarriorTargeting>().enabled = true;
GameObject.Find("Mage").GetComponent<MageHealth>().enabled = false;
GameObject.Find("Mage").GetComponent<MageTargeting>().enabled = false;
GameObject.Find("Rouge").GetComponent<RougeHealth>().enabled = false;
GameObject.Find("Rouge").GetComponent<RougeTargeting>().enabled = false;
}
private void DeselectTarget1()
{
if (selectedTarget1)
{
selectedTarget1 = null;
}
}
private void SelectTarget2()
{
mage = false;
warrior = false;
archer = true;
rouge = false;
GameObject.Find("Archer").GetComponent<ArcherTargeting>().enabled = true;
GameObject.Find("Archer").GetComponent<ArcherHealth>().enabled = true;
GameObject.Find("Warrior").GetComponent<WarriorHealth>().enabled = false;
GameObject.Find("Warrior").GetComponent<WarriorTargeting>().enabled = false;
GameObject.Find("Mage").GetComponent<MageHealth>().enabled = false;
GameObject.Find("Mage").GetComponent<MageTargeting>().enabled = false;
GameObject.Find("Rouge").GetComponent<RougeHealth>().enabled = false;
}
private void DeselectTarget2()
{
if (selectedTarget2)
{
selectedTarget2 = null;
}
}
private void SelectTarget3()
{
mage = false;
warrior = false;
archer = false;
rouge = true;
GameObject.Find("Archer").GetComponent<ArcherHealth>().enabled = false;
GameObject.Find("Archer").GetComponent<ArcherTargeting>().enabled = false;
GameObject.Find("Warrior").GetComponent<WarriorHealth>().enabled = false;
GameObject.Find("Warrior").GetComponent<WarriorTargeting>().enabled = false;
GameObject.Find("Mage").GetComponent<MageHealth>().enabled = false;
GameObject.Find("Mage").GetComponent<MageTargeting>().enabled = false;
GameObject.Find("Rouge").GetComponent<RougeHealth>().enabled = true;
GameObject.Find("Rouge").GetComponent<RougeTargeting>().enabled = true;
}
private void DeselectTarget3()
{
if (selectedTarget3)
{
selectedTarget3 = null;
}
}
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;
GetComponent<WarriorEquipedItems>().enabled = false;
GetComponent<ArcherEquipedItems>().enabled = false;
GetComponent<RougeEquipedItems>().enabled = false;
}
GetComponent<MageStats>().enabled = false;
GetComponent<WarriorStats>().enabled = false;
GetComponent<ArcherStats>().enabled = false;
GetComponent<RougeStats>().enabled = false;
}
GetComponent<MageAbilities>().enabled = false;
GetComponent<WarriorAbilities>().enabled = false;
GetComponent<ArcherAbilities>().enabled = false;
GetComponent<RougeAbilities>().enabled = false;
}
It's only reading the first set of brackets, which is button1Clicked = true
, etc, or it's only reading up to "GetComponent().enabled = true;
?
Sry its only reading the GEtcomponent().enabled = false one but its only reading the first one/line and none of the others
Uh...now you lost me. Retyping what again? I thought this was about parts of your code not getting executed. So what exactly are you editing?
We still don't understand what exactly happends because what you say makes no sense. Your code is compiled into something your computer can execute. If it compiles without any errors that means the code will execute in the exact order you've arranged it.
The only thing that might interrupt the execution is when an exception occurs. This can happen when, for example, the component you try to get with GetComponent isn't attached to this gameobject. If an exception isn't handled by your code it will fall through until it reaches Unity and Unity will display an error.
That means: When you don't get an error and you don't catch the exception yourself (with a try-catch block), there's no reason which could interrupt the execution.
What Wolfram was asking have you added a Debug.Log("Yeah, i reached this line"); at the end of your code block to see it reaches the line or not?
You just posted a code fragment. We have no idea to what class this fragment belongs. It's probably inside a OnGUI function.
Furthermore your "second pair of brackets" are completely useless. I reformatted your code so it becomes a bit more clear.
It seems you have a quite complex GameObject since you have a lot scripts attached to it. We know nothing about your scene, your setup or anything about your situation except your 2.4 lines of description above.
So can you please improve your question because it's obvious that no one understand what's your problem. Feel free to edit your question and include all relevant code and give some more information what debugging steps you've already tried. We can't debug your stuff for you since we don't have your project.
What's with the 5 nested code blocks starting on line 75? They are unnecessary and do nothing, except making the code harder to read (especially since the rest of your indenting isn't consistent anyway).
Concerning code execution they should not matter, but your claim the code folliwing the first block doesn't get executed suggests otherwise. So remove all nested { and }.
Also, inserting Debug.Log() at key points in your code is the very first thing you should try to check whether (and when and how often) a certain code passage is getting executed or not.
Wild guess: you have other scripts or code fragments that re-enable the components you are trying to switch off, so it seems the code you posted is not getting executed while in fact it is, but it's immediately overridden by some other script or code segment.