- Home /
Reassign Variable Value C#
Hello, I am currently making my first 2D game in Unity using C#. It is a stealth game in which the enemy character usually patrols and if the player is in the enemy's sight, the enemy chases the player. For the sight, I have added a child game object to the enemy with a trigger collider, in the shape of a cone. This is the code for the sight:
public class Sight : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
gameObject.GetComponentInParent<Enemy>().checkIn(true);
}
}
The checkIn function is in the parent enemy class:
public class Enemy : MonoBehaviour
{
bool isIn = false;
void Start()
{
rigidBody = GetComponent<Rigidbody2D>();
}
void Update()
{
if(isIn)
{
chase();
}
else
{
patrol();
}
//method prints the boolean in string form
Debug.Log(method(isIn));
}
public void checkIn(bool c)
{
isIn = c;
}
}
I haven't included all of the code in this, but what I am trying to do is to change the value of isIn using the checkIn function, however, using the debug line I understand that the function checkIn does not reassign the value of isIn and it remains false. I have looked into using the ref keyword, by using ref isIn = c. However, I get compiler errors from Unity. So I would appreciate if you could guide me to a solution. Thanks.
Answer by Bunny83 · Jul 30, 2019 at 12:10 PM
It's ok to not include all code, however the code you provided doesn't seem to be related in any way. Inside your Sight class you call the checkIn method of a class called KnightController. You showed a class called Enemy. Is the KnightController class derived from Enemy? If so are you sure there is only one Enemy derived class on your gameobject?
What you should do is adding a Debug.Log to your "checkIn" method (which btw has a terrible name)
public void checkIn(bool c)
{
Debug.Log("checkIn("+c+") old value was " + isIn, gameObject);
isIn = c;
}
This log message does several things. First of all the message contains detailed information so you know for sure that it's this line. Just logging "true" or "false" is missing the context. I've seen many people logging just a boolean or float value without any additional text. If you do this in two or more places you completely loose track which log belongs to which message.
This debug log message tells you exactly what parameter was passed to your method and also what the previous value of the variable was. So you know what it was and what it is set to. In addition I pass "gameObject" as context parameter to Debug.Log. This allows you / Unity to identify the object that actually produced this log message be just clicking once on the log message. Unity will ping that object in the hierarchy / project panel. This should help to figure out if you actually access the correct object. Many times people accidentally call methods on prefabs instead of the actual object instances in the scene.
Yes sorry, I realised that $$anonymous$$nightController should have been Enemy, I've changed that.
Thanks, this helped. I realised that the trigger collider was detecting the obstacles in the game and not the player.
Answer by Dawdlebird · Jul 30, 2019 at 11:15 AM
Why are you debug logging "method(isIn)"? You can just Debug.Log(isIn). It will log true or false. If you're successfully calling checkIn() with some value, your log should log the proper bool value. Perhaps the problem lies with whatever calls checkIn() (you're not calling it anywhere in your example here)
Thanks, I'll change that. I thought that checkIn() was being called by the Sight class when the player enters the trigger collider. The console only shows false even when the player enters the trigger collider. So does that mean that the OnTriggerEnter2D method does not call the checkIn() method?
Oh, I'm sorry, I actually missed your ontrigger code >_<
Your answer
Follow this Question
Related Questions
Very, very strange float assignment problem. 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can I choose what to pass into .getcomponent<{VARIABLE}>(); 1 Answer
Assigning variables in inspector 1 Answer