Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

c# - Unity : Variable showing false but it's true

I have a problem. In my C# script I have a variable named "topTouched", that is false by default. It becomes true if a y position is reached. the problem is : In an if, one condition is to "topTouched" have to be true. But when I print a Log with that variable. It shows false. I don't understand. Thanx for your help. :3

Here is the problem :

if (-5f > Mace.transform.position.y || topTouched == true)
        {
            print("hey1" + topTouched);
            rb.AddForce(new Vector2(0f, -10f));
        }

Complete code :

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

public class Moves : MonoBehaviour
{

public Rigidbody2D rb;
private GameObject Mace;
private bool topTouched;

void Start()
{
    Mace = this.gameObject;
}

void Update()
{
    if(Mace.transform.position.y == -7.5f)
    {
        topTouched = true;
    }
    if (Mace.transform.position.y == 3.3f)
    {
        topTouched = false;
    }

    if (Mace.transform.position.y != 3.4f || Mace.transform.position.y != -7.6f)
    {
        if( -5f > Mace.transform.position.y || topTouched == false)
        {
            print("hey_" + topTouched);
            rb.AddForce(new Vector2(0f, 10f));
        }
        if (-5f > Mace.transform.position.y || topTouched == true)
        {
            print("hey1" + topTouched);
            rb.AddForce(new Vector2(0f, -10f));
        }
        if (-5f < Mace.transform.position.y || topTouched == true)
        {
            print("hey2" + topTouched);
            rb.AddForce(new Vector2(0f, -10f));
        }
        if (-5f < Mace.transform.position.y || topTouched == false)
        {
            print("hey3" + topTouched);
            rb.AddForce(new Vector2(0f, 10f));
        }
    }
}
}

In unity console, it shows :

Sceenshot unity console


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...