Hello, I’m making a game where you’re a little character moving through a guitar fret board, my goal right now is to be able to press an interact button and have it play the right note based on which fret you’re in.
My idea is to have individual objects with a trigger box collider in each fret, and have OnTriggerEnter2D call a function in my character controller that sets a variable to a number so that later I can play the correct sound based on that number.
I would have this code in every object where fretNum is a different int for every individual one
void OnTriggerEnter2D (Collider2D collision)
{
CharacterController2D player = collision.GetComponent<CharacterController2D>();
if(collision != null)
{
player.SetPos(fretNum);
}
}
and in my character controller I have
int pos;
public void SetPos(int currentPos)
{
currentPos = pos;
Debug.Log(pos);
}
But in my console it just prints out the number 0 no matter what value I set fretNum. It also prints twice, which I don’t understand. Is there something I’m doing wrong? Is there a different way you would approach a situation like this?
Thanks in advance.