C# value switching value without me touching it

2019-08-21 16:25发布

问题:

I'm developing a game in Unity using C#. A part of the code, using a boolean, decides if the player is allowed to buy an upgrade for a building or not. If he is, the integer of the building is increased by 1. Next to that, the boolean gets set to false. If he isn't, nothing happens at the moment.

There is a button in game, functionally increasing the week number by 1, allowing the player again to be able to upgrade building (thus setting the boolean true again).

The problem is that after buying the upgrade, the boolean is set to false, and even when I set it back to true again, when I try to upgrade the building, the boolean appears to be false. I also tried to check it with the help of an integer instead of a boolean (using the values 0 and 1), but the problem is still here.

Here is some code:

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

public class ActivateTrigger : MonoBehaviour
{
    public int transtech = 0;
    public int factech = 0;
    public bool facmag = true;
    public bool transmag = true;

    void OnGUI () {GUI.Box(new Rect(0, Screen.height - 120 - 60, 150, 30), "Week: " + week.ToString());
    if (GUI.Button(new Rect(0, Screen.height - 120 - 30, 150, 20), "Continue one week"))
    {       
        NextProcess();
    }
    if (GUI.Button(new Rect(20, 70, 120, 20), "Koop upgrade") )
    {
            Debug.Log("Facmag: " + facmag);
        if (facmag == true)
            {
        facmag = false;
            }
    }

    void NextProcess()
    {
        Debug.Log("Next process called");
        facmag = true;
        transmag = true;
        Debug.Log("Facmag: " + facmag);
    }
}

So, when NextProcess() is called, the Debug Log shows the value being true. But after the first time buying the upgrade, no matter if I pressed the button to execute NextProcess() or not, the value stays false. I'm wondering if I missed something completely or not. No other code in this class uses the booleans, nor do any of the few other classes I've got do. So where lies the problem?

回答1:

[Short Answer]

If your script is attached to more than 1 object only the top most button in OnGUI() is modifying facmag or calling NextProcess().

[Slight Expansion of my comment above]

Unity's order of execution for scripts shared between objects is not controllable. (You can modify script order in a single object though.) That means OnGUI() will be called N times for the N objects it's attached to, in random order. Only the "top most" GUI element at a particular mouse position will accept mouse input.

Example:

using UnityEngine;
using System.Collections;

public class OverlapButton : MonoBehaviour {
    public string id_;
    void OnGUI(){
            if(GUI.Button(new Rect(0,0,50,50),"Touch")){
                    Debug.Log("I'm object " + id_);
            }
    }
}

If you attach this script to 50 objects, Unity will draw 50 buttons but only 1 button will respond to a mouse click and you can't control which one.