Ok, so I have set a public int to my c# script as follows:
public int isEnemy;
I have changed the value on the actual object to “1”.
I have other objects which are set to ‘0’ (not an enemy).
I want to use this int in another script to check whether or not the object is an “enemy” based on the value i manually set.
The method I used, as the int is not “static” - was to create another ‘static’ version:
public static int yesEnemy;
And then in a Start() function, try and set them to be the same:
void Start()
{
yesEnemy = isEnemy;
}
But when I try to call “yesEnemy” from my other script to check whether it is “1” - it always returns as “0”…
Can anyone help me figure this out please as I’ve been unsuccessful :(. I’m very new to all this so I’m just learning at the moment… It’s probably very obvious.
Thanks in advance~
I wouldn’t say the issue is ‘very obvious’ because it is not obvious why you are trying to do it this way when booleans exist…
I was just going to link you to the documentation on booleans but I genuinely cannot find it so perhaps this is why you haven’t used one instead.
public bool isEnemy;
private bool toggleExample;
void Start()
{
if(isEnemy == true)
{
//Shoot the enemy
}
else
{
//Don't shoot the enemy
}
toggleExample = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
toggleExample = !toggleExample;
print(toggleExample);
}
}
Without knowing where the static variable is, why you need a static, if one script is inheriting from the other or has a reference to it etc your problem can’t really be solved. 0 is the default value of an int just as false is the default value of a bool so whatever you’re doing isn’t having an effect, that’s all that is certain.
Chances are you probably want to use GetComponent or SendMessage or a reference to the script of the other object to get the state of isEnemy from it. Or just use an “Enemy” tag. Depends when and how you need to check it.
What is certain however is that if you only need a 0 and 1 state then use a bool instead.