更新后,变量会重置为零(Universe,C#)

时间:2018-10-26 23:08:47

标签: c# variables unity3d

很抱歉,我的编码不好,过了一会儿我才重新使用它,并且我意识到可以对其进行优化。

主要目标是设置我的所有变量,然后在发生更改时希望将其宣布。这就是我所拥有的,在一次更新之后,它们都被设置为零。

更深入的方法:我在开始时设置了“新”变量,这些变量用于更新,使其等于初始变量。之后,我检查每次更新时“新”变量是否与起始变量相同,如果不是,则让我读出“新”变量是什么,然后用“新变量”替换起始变量” vars。这样一来,任何区别都只会被发现一次。

我的代码解释:这花了一段时间,但我认为它将起作用,例如,上半部分给出了我这次设置的变量变量,但稍后将更像强度和速度他们会随着游戏玩法而改变。然后,我创建将在更新中使用的整数变量。游戏开始时,它将读取变量,它们都是正确的。然后,我在开始时设置“新”变量,该变量用于更新,使其等于初始变量。之后,我检查每次更新时“新”变量是否与起始变量相同,如果不是,则让我读出“新”变量是什么,然后用“新变量”替换起始变量” vars。这样一来,任何区别都只会被发现一次。

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

public class MainCharacterVarsScript : MonoBehaviour 
{
    //These are base character stats
    private int CharacterLife = 100; //  out of 125
    private int CharacterStamina = 100; // out of 100
    private int CharacterSight = 12; // out of 40
    private int CharacterHunger = 100; // out of 100
    private int CharacterExp = 0; // out of 100,000
    private float CharacterStrength;

    //These are the update stats
    int NewCharacterLife;
    int NewCharacterStamina;
    int NewCharacterSight;
    int NewCharacterHunger;
    int NewCharacterExp;

    float NewCharacterStrength;

    // Use this for initialization
    void Start () 
    {
        int CharacterLife = 100; //  out of 125
        int CharacterStamina = 100; // out of 100
        int CharacterSight = 12; // out of 40
        int CharacterHunger = 100; // out of 100
        int CharacterExp = 0;

        float CharacterStrength = ((CharacterHunger * .2f) + (CharacterLife * .15f) + (CharacterStamina * .05f)); // out of 40
        float CharacterSpeed = ((CharacterHunger * .1f) + (CharacterLife * .25f) + (CharacterStamina * .05f)); // out of 40

        Debug.Log ("Character life is " + CharacterLife);
        Debug.Log ("Character Strength is " + CharacterStrength);
        Debug.Log ("Character Stamina is " + CharacterStamina);
        Debug.Log ("Character sight is " + CharacterSight);
        Debug.Log ("Character Hunger is " + CharacterHunger);
        Debug.Log ("Character EXP is " + CharacterExp);
        Debug.Log ("Character Speed is " + CharacterSpeed);

        int NewCharacterLife = CharacterLife;
        int NewCharacterStamina = CharacterStamina;
        int NewCharacterSight = CharacterSight;
        int NewCharacterHunger = CharacterHunger;
        int NewCharacterExp = CharacterExp;

        float NewCharacterStrength = CharacterStrength;
    }

    // Update is called once per frame
    private void Update () 
    {
        float CharacterStrength = ((CharacterHunger * .2f) + (CharacterLife * .15f) + (CharacterStamina * .05f)); // out of 40
        float CharacterSpeed = ((CharacterHunger * .1f) + (CharacterLife * .25f) + (CharacterStamina * .05f)); // out of 40

        if (CharacterLife != NewCharacterLife) 
        {
            Debug.Log ("Character life is now " + NewCharacterLife);
            NewCharacterLife = CharacterLife;
        } 
        else if (CharacterStamina != NewCharacterStamina) 
        { 
            Debug.Log ("Character Stamina is now " + NewCharacterStamina); 
            CharacterStamina = NewCharacterStamina;
        } 
        else if (CharacterSight != NewCharacterSight) 
        { 
            Debug.Log ("Character Sight is now " + NewCharacterSight); 
            CharacterSight = NewCharacterSight;
        } 
        else if (CharacterHunger != NewCharacterHunger) 
        {
            Debug.Log ("Character Hunger is now " + NewCharacterHunger); 
            CharacterHunger = NewCharacterHunger;
        }
        else if (CharacterExp != NewCharacterExp) 
        {
            Debug.Log ("Character EXP is now " + NewCharacterExp);
            CharacterExp = NewCharacterExp;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

对于我在稍加修改后不久才看到它的人来说,这似乎是暂时的:

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

public class MainCharacterVarsScript : MonoBehaviour 
{
    //These are base character stats

    private int CharacterLife = 100; //  out of 125
    private int CharacterStamina = 100; // out of 100
    private int CharacterSight = 12; // out of 40
    private int CharacterHunger = 100; // out of 100
    private int CharacterExp = 0; // out of 100,000
    private float CharacterStrength;

    //These are the update stats

    int NewCharacterLife;
    int NewCharacterStamina;
    int NewCharacterSight;
    int NewCharacterHunger;
    int NewCharacterExp;

    float NewCharacterStrength;

    // Use this for initialization
    void Start () 
    {
        float CharacterStrength = ((CharacterHunger * .2f) + (CharacterLife * .15f) + (CharacterStamina * .05f)); // out of 40

        float CharacterSpeed = ((CharacterHunger * .1f) + (CharacterLife * .25f) + (CharacterStamina * .05f)); // out of 40

        Debug.Log ("Character life is " + CharacterLife);
        Debug.Log ("Character Strength is " + CharacterStrength);
        Debug.Log ("Character Stamina is " + CharacterStamina);
        Debug.Log ("Character sight is " + CharacterSight);
        Debug.Log ("Character Hunger is " + CharacterHunger);
        Debug.Log ("Character EXP is " + CharacterExp);
        Debug.Log ("Character Speed is " + CharacterSpeed);

        NewCharacterLife = CharacterLife;
        NewCharacterStamina = CharacterStamina;
        NewCharacterSight = CharacterSight;
        NewCharacterHunger = CharacterHunger;
        NewCharacterExp = CharacterExp;
        NewCharacterStrength = CharacterStrength;
    }

    // Update is called once per frame
    void Update () 
    {
        float CharacterStrength = ((CharacterHunger * .2f) + (CharacterLife * .15f) + (CharacterStamina * .05f)); // out of 40

        float CharacterSpeed = ((CharacterHunger * .1f) + (CharacterLife * .25f) + (CharacterStamina * .05f)); // out of 40

        if (CharacterLife != NewCharacterLife) 
        {
            Debug.Log ("Character life is now " + NewCharacterLife);
            NewCharacterLife = CharacterLife;
        } 
        else if (CharacterStamina != NewCharacterStamina) 
        { 
            Debug.Log ("Character Stamina is now " + NewCharacterStamina); 
            CharacterStamina = NewCharacterStamina;
        } 
        else if (CharacterSight != NewCharacterSight) 
        { 
            Debug.Log ("Character Sight is now " + NewCharacterSight); 
            CharacterSight = NewCharacterSight;
        } 
        else if (CharacterHunger != NewCharacterHunger) 
        {
            Debug.Log ("Character Hunger is now " + NewCharacterHunger); 
            CharacterHunger = NewCharacterHunger;
        }
        else if (CharacterExp != NewCharacterExp) 
        {
            Debug.Log ("Character EXP is now " + NewCharacterExp);
            CharacterExp = NewCharacterExp;
        }
    }
}