代码假装不存在变量

问题描述 投票:-4回答:3

基本上,我需要一次性检查音频片段的长度,然后将其存储在变量中,以用作代码在继续和销毁游戏对象之前应该等待的时间长度。

我已经尝试了很多不同的东西,比如把它带到if语句之外,但是,每次尝试都会导致这个代码工作,但不是按照预期或只是拒绝编译。

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

 public class Fighting : MonoBehaviour
{

public int Team = 1; //Which team entity is on
public int Health = 100; //How much heath it has
public int MaxHealth = 100; //Maximum health cap
public int AttackDamage = 10; //Attack Damage to enemy entities
public int HealAmount = 0; //Amount it can heal friendly entities
public int DecayAmount = 1; //Amount of health lost over time (If over health cap)
public float DecayCooling = 1; //Seconds it takes for health to be lost over time (If over health cap)
public float DecayOriginal = 1; //Needs to be hidden and equal to DecayCooling
public float CoolDown = 2; //Seconds it takes until it can attack enemies again
public float original = 2; //Needs to be hidden and equal to CoolDown
public bool CoolingDown = false; //Is attack cooling Down?
public bool Decaying = false; //Is decaying cooling Down?
public bool Structure = false; //Is it a building?
public bool SelfHealing = false; //Can it heal itself?
public bool HealAll = false; //Can it heal every friendly unit in its radius?
public bool Garrisoning = false; //Can it garrison troops?
public bool Snap = false; //Do you wanna snap it?
public bool clipplayed = false;
public bool lengthset = false;
public AudioSource aSource;
//public AudioSource[] mSource;
public Component[] obj;

// Update is called once per frame
void Update()
{
    if (Snap == true || Health <= 0) //Snaps the entity if either is true
    {
        //Destroy(gameObject, .5f);
        obj = transform.parent.GetComponentsInChildren<MeshRenderer>();
        foreach (MeshRenderer rend in obj)
        {
            rend.enabled = false;
        }

        if (clipplayed == false)
        {
            aSource.Play();
            clipplayed = true;
        }

        bool playing = true;

        if (playing == false)
        {
            Destroy(transform.parent.gameObject);
        }

        if (playing == true)
        {
            if (lengthset == false)
            {
                float playtimer = aSource.clip.length;
                lengthset = true;
            }

            playtimer -= Time.deltaTime;

            if (playtimer <= 0)
            {
                playing = false;
            }

        }
    }

    if (Input.GetKey(KeyCode.N)) Instantiate(transform.parent.gameObject); //Debug tool to spawn another

    if (Health > MaxHealth && Decaying == false) //Checks to see if health should be decaying
    {
        Health -= DecayAmount;
        Decaying = true;
    }
    if (Decaying == true)
    {
        DecayCooling -= Time.deltaTime;
        if (DecayCooling <= 0)
        {
            Decaying = false;
            DecayCooling = DecayOriginal;
        }
    }
}

}

这些代码行:

playtimer - = Time.deltaTime;

if(playtimer <= 0)

应该可以看到:

float playtimer = aSource.clip.length;

相反,他们假装它甚至不存在,并且脚本不会因为它而编译。

c# unity3d
3个回答
0
投票

你在if范围内声明播放时间,所以当你调用playtimer时它不存在 - = Time.deltaTime;

这样看,当lengthset不是false时会发生什么,你还没有声明播放时间:

if (lengthset == false)
{
    float playtimer = aSource.clip.length;
    lengthset = true;
}
playtimer -= Time.deltaTime;

相反,在if范围之外声明它:

float playtimer = 0;
if (lengthset == false)
{
    playtimer = aSource.clip.length;
    lengthset = true;
}
playtimer -= Time.deltaTime;

哦,天哪,从你的评论中说:“我以这种方式构建了代码,这样它只执行一次而不仅仅是将变量设置回原来的状态。是否有更好的方法来实现我的意图?”。

如果您需要在调用之间跟踪playtimer的值,那么您可能希望将其添加为实例变量(即,顶部包含所有其他变量)。如果这样做,请确保从方法逻辑中删除声明。


1
投票

playtimer变量定义为更高一级,以便在两个位置都能看到它。

float playtimer = 0;
if (lengthset == false)
{
    playtimer = aSource.clip.length;
    lengthset = true;
}

playtimer -= Time.deltaTime;

0
投票

在C#中,嵌套作用域中的代码可以查看在作用域外定义的变量。范围之外的代码无法查看范围内定义的变量。

因此,以下方法不起作用:

{
    int a = 5;
}

Console.WriteLine(a); // can't see a because it's declaration is in the nested scope

但这会奏效:

int a = 0;

{
    a = 5;
}

Console.WriteLine(a); // can see a, its value is 5

在此示例中,a在两个嵌套作用域之外声明,因此它们都可以访问:

{
    int a = 5;

    {
        a = 3;
    }

    {
        Console.WriteLine(a); // this will also work because a is declared outside the scope. Its value is 3.
    }
}

如果在调用playTimer之间需要Update()为相同的变量(即包含相同的值),则需要将其声明移到Update()的范围之外并进入类级别范围的范围。

例如:

class Test
{
    float playTimer = 0;

    void Update()
    {
        Console.WriteLine(playTimer);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.