QuestionHelpHow Can I Make An InGame Variable (Integer) [Help]

Posts 13 of 3 · Page 1 of 1
How Can I Make An InUnity3DGame Variable (Integer) [Help]
I want to make a variable that will work In The Game . I mean I tryed this :
Code:
public class flashing : MonoBehaviour {
	// Update is called once per frame
	void Update()
    {
		int points = 0;
		if(Input.GetKeyDown(KeyCode.O)){
			points += 1;
		}
        if (Input.GetKeyDown(KeyCode.F))
        {	
            if(light.enabled == true && points == 5){
				light.enabled = false;
				points -= 5;
			}
			else if(light.enabled == false && points == 5){
				
				light.enabled = true;		
				points -= 5;
			}
		}
	}
}
and it didn't work . I don't know why . There were not any errors .
The project I want to do is ... When I have 5 points to have the permission to close or open the lights , if not then I have to gather points pushing button "O" . Can anyone help me ?
You are resetting points every call cause its defined inside the function and is not static. Try something like this:
Code:
public class flashing : MonoBehaviour
{
    void Update()
    {
        static int points = 0;
        
        if (Input.GetKeyDown(KeyCode.O))
            points++;
            
        if (Input.GetKeyDown(KeyCode.F) && points == 5)
        {
            points = 0;
            light.enabled = !light.enabled;
        }
    }
}
I tryed with "static" keyword but unity sends me an error ... Unexpected Symbol 'static'
Posts 13 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?