По сути, мне нужно выполнить однократную проверку длины аудиоклипа, а затем сохранить ее в переменной, чтобы использовать в качестве промежутка времени, в течение которого код должен ждать, прежде чем продолжить и уничтожить игровой объект.
Я пробовал множество разных вещей, например, вывод его за пределы оператора 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) </p>
Должен быть в состоянии увидеть:
float playtimer = aSource.clip.length;
Вместо этого они делают вид, будто его даже не существует, и сценарий не будет компилироваться из-за него.