Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 代码假装变量不存在';不存在_C#_Unity3d - Fatal编程技术网

C# 代码假装变量不存在';不存在

C# 代码假装变量不存在';不存在,c#,unity3d,C#,Unity3d,基本上,我需要对音频片段的长度进行一次性检查,然后将其存储在变量中,作为代码在继续并销毁游戏对象之前应该等待的时间长度 我尝试过很多不同的方法,比如把它带到if语句之外,但是,每次尝试都会导致这段代码要么正常工作,要么不按预期运行,要么就是拒绝编译 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Fighting : MonoBehaviour { pub

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

我尝试过很多不同的方法,比如把它带到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;
        }
    }
}

}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共阶级斗争:单一行为
{
public int Team=1;//使用哪个团队实体
public int Health=100;//它有多少健康
public int MaxHealth=100;//最大健康上限
public int AttackDamage=10;//对敌人实体的攻击伤害
public int HealAmount=0;//它可以修复友好实体的数量
public int DecayAmount=1;//随时间损失的健康量(如果超过健康上限)
public float DecayCooling=1;//随着时间的推移,运行状况损失所需的秒数(如果超过运行状况上限)
公共浮点DecyOriginal=1;//需要隐藏并等于DecyCooling
公共浮标冷却时间=2;//需要几秒钟才能再次攻击敌人
public float original=2;//需要隐藏并等于冷却时间
public bool CoolingDown=false;//攻击是否正在冷却?
public bool Decaying=false;//衰减正在冷却吗?
public bool Structure=false;//它是一栋建筑吗?
public bool SelfHealing=false;//它能自愈吗?
public bool HealAll=false;//它能治疗半径内的所有友军单位吗?
public bool Garrisoning=false;//它可以驻军吗?
public bool Snap=false;//是否要捕捉它?
public bool clipplayed=false;
公共布尔长度集=false;
公共音频源;
//公共音频源[]MSSource;
公共部门[]obj;
//每帧调用一次更新
无效更新()
{

如果(Snap==true | | HealthDefine
playtimer
变量的级别更高,可以在两个位置看到它

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

playtimer -= Time.deltaTime;

您正在if范围内声明playtimer,因此调用时它不存在 playtimer-=Time.deltaTime

这样看,如果lengthset不为false,则不会声明playtimer:

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的值,那么您可能希望将其添加为实例变量(即,与所有其他变量一起添加).如果要删除方法逻辑中的声明。

在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
在本例中,在两个嵌套作用域之外声明了,因此两个嵌套作用域都可以访问它:

{
    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);
    }
}

如果lengthset==true怎么办?没有声明playtimer的类型。那么我应该如何在保留预期功能的同时解决问题呢?我以这种方式构建代码,使其只执行一次,而不只是反复将变量设置回原来的状态。有没有更好的方法来实现我的意图?如果(playing==false)将永远不会被调用。您正在声明变量bool playing=true;如上所述it@Toxic您的意思是变量playtimer在对Update()的调用之间应该相同吗?当代码进行这些调整时,它不能按预期工作。由于某些原因,播放从未设置为false,因此代码只是无限循环。@Toxic
playing
仅存在于您的
Update()中
method,因此每次调用您的方法时,它都将初始化为
true
。但是,这超出了本问题的范围。