- Home /
Need help with changing bool depending on what array is shooting at
Basicly I want my crosshair to change depending on if crosshairenemy is true or false , problem is when it changes to true it dosent want to go back to false .
public float MouseX;
public float MouseY;
public float MouseSensX = 5f;
public float MouseSensY = 5f;
public Camera MainCamera;
public Texture2D crosshair;
public Texture2D _crosshairenemy;
public bool crosshairenemy;
public int damage = 10;
public float firerate = 0.25f;
public float range = 50f;
void Start () {
}
void Update () {
MouseX = Input.GetAxis("Mouse X");
MouseY = Input.GetAxis("Mouse Y");
transform.Rotate(new Vector3(0 ,MouseX * Time.deltaTime * MouseSensX, 0));
MainCamera.transform.Rotate(new Vector3(-MouseY * Time.deltaTime * MouseSensY,0, 0));
menjajCrosshair();
if(Input.GetMouseButtonDown (0))
{
Pucaj();
}
}
void Pucaj()
{
RaycastHit info;
if(Physics.Raycast(MainCamera.transform.position, MainCamera.transform.forward, out info))
{
Debug.Log(info.transform.name);
Enemy enemy = info.transform.GetComponent<Enemy>();
if(enemy != null)
{
enemy.takeDamage(damage);
}
}
}
void menjajCrosshair()
{
RaycastHit info;
if(Physics.Raycast(MainCamera.transform.position, MainCamera.transform.forward, out info))
{
if (info.transform.name == "Enemy")
{
crosshairenemy = true;
// Debug.Log("True");
}
else if(info.transform.name == null)
{
crosshairenemy = false;
// Debug.Log("False");
}
}
}
void OnGUI()
{
float xMin = (Screen.width / 2) - (crosshair.width / 2);
float yMin = (Screen.height / 2) - (crosshair.height / 2);
if (crosshairenemy == false)
{
GUI.DrawTexture(new Rect(xMin, yMin, crosshair.width, crosshair.height), crosshair);
}
if(crosshairenemy == true)
{
GUI.DrawTexture(new Rect(xMin, yMin, crosshair.width, crosshair.height), _crosshairenemy);
}
}
}
Answer by lorenzofman2 · May 18, 2018 at 09:04 PM
The problem is here:
else if(info.transform.name == null)
(Line 59)
You're testing if the name is not "Enemy" and also if the name is equal to null (empty).
Probably you want to change that to just an else
Then when you aim at an enemy it will change the crosshair to the enemy one and when you do not it will change back. The way it is now it will only come back to normal when you aim at a object with no name.
Your answer
![](https://koobas.hobune.stream/wayback/20220612162946im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Array Of Bools 4 Answers
Mysterious crash involving an array 2 Answers
Custom Inspector GUI, Instantiating some classes 2 Answers
Initialize boolean array as all true Unity JS 2 Answers
TurnBased RPG Help, Checking booleans with an array. 0 Answers