❓ In Game Variable not Workin' ...

Posts 1–4 of 4 · Page 1 of 1
❓ In Game Variable not Workin' ...
I just want when I push "F" the flashlight to turn on ONLY if I have 5 points . And when I push "O" to gain Points so I can use the flashlight . But if I try the "int points = 0 " to make it "static int points = 0" it doesn't work . It sends me an error "Unexpected Symbol 'static' . In this code I put the 'static' in brackets just to remind you I have used it without and with it in this code .
Code:
using UnityEngine;
using System.Collections;

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;
        }
    }
}
Try defining the points outside of the function then as part of the class. I'm not sure if Unity puts limitations on the code it runs inside its scripts or not.
Also you should check if greater than or equal to 5 for the flash light. Since you don't cap how much points O will generate.

Code:
using UnityEngine;
using System.Collections;

public class flashing : MonoBehaviour
{
    private static int _points;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.O))
            _points++;
            
        if (Input.GetKeyDown(KeyCode.F) && _points >= 5)
        {
            _points -= 5;
            light.enabled = !light.enabled;
        }
    }
}
Quote Originally Posted by atom0s View Post
Try defining the points outside of the function then as part of the class. I'm not sure if Unity puts limitations on the code it runs inside its scripts or not.
Also you should check if greater than or equal to 5 for the flash light. Since you don't cap how much points O will generate.

Code:
using UnityEngine;
using System.Collections;

public class flashing : MonoBehaviour
{
    private static int _points;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.O))
            this._points++;
            
        if (Input.GetKeyDown(KeyCode.F) && this._points >= 5)
        {
            this._points -= 5;
            light.enabled = !light.enabled;
        }
    }
}
Nope ... it's not working too ... -_- This unity ... The error :
"CS0176: Static member 'flashing.points' cannot be accessed with an intance reference, qualify it with a type name instead"
Thank you for the supporting dude ... But I want to fint a solution to this issue very much . If I will find a solution I will be able to do more things . I am a nearly beginner and I want to learn more .
Oh blah that was my bad, and what I get for not thinking after just waking up.
Remove the this. keywords from the _points given that it is static.

Updated the code above.
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Talk with us