Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unity/C# If-Statement is always true

Why is this always true?

public float speed;
    bool working = false;
    
    // Start is called before the first frame update
    void Start()
    {
        skilling = false;
        speed = 3f;
        
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 playerInput = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), 0);
        transform.position = transform.position + playerInput.normalized * speed * Time.deltaTime;
        
        if(speed >= 500f)
            {
                print("test");
            }
    }

originally i wanted to make the condition based on the "working" boolean, aswell as Key Input. It was always true.

I am losing my mind..

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

test
test
test
test
test
test

I tried the If-Statement with the Float as in the example, with a bool and also with a collision. It was always true. I must have messed up somewhere, but i cannot find it.

Edit:
Complete Code:
`

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class player : MonoBehaviour
{
    float speed;
    bool working = false;
    
    // Start is called before the first frame update
    void Start()
    {
        
        speed = 3f;
        
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 playerInput = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), 0);
        transform.position = transform.position + playerInput.normalized * speed * Time.deltaTime;
        
        if(speed >= 500f)
            {
                print("test");
            }
    }
    /*void OnTriggerEnter2D(Collider2D other) {
        if(other.tag == "Skill")
            {
                //skilling = true;
            }
        else skilling = false;
    }*/
}

`
I removed the public from the speed variable, still true.

>Solution :

Possible solutions:

1- Make sure you did not modify the speed variable from the inspector since it’s public.

2- Make sure other classes are not changing the speed variable at the start.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading