所以我在玩游戏中引用一个物体时遇到了麻烦,而不是一个预制的Ver简单代码,它会在玩家击中时炸毁汽车。球体对撞机在爆炸时会被打开以造成伤害
所以会发生的情况是,当场地中有2辆或更多车时,如果玩家击中其中一辆,那么第一辆投入使用将会爆炸,然后任何进一步的尝试击中他们的锁定,因为游戏对象没有已经存在了。我知道问题是引用游戏中的对象而不是预制件,但我该怎么做
爆炸对象上的2个代码
public class ObjectExplode : MonoBehaviour
{
// This goes on objects that will be destroyed that hurt tags.
[SerializeField] AudioSource explosionSound;
[SerializeField] GameObject explosionPrefab;// Set the explosion particle
[HideInInspector] public bool explode = false;// make the object explode
private SphereCollider sphereCollider;
void Start()
{
sphereCollider = GetComponent<SphereCollider>();
sphereCollider.enabled = false;
}
public void Explode()
{
sphereCollider.enabled = true;
GameObject explosionClone = (GameObject)Instantiate
(explosionPrefab, gameObject.transform.position, gameObject.transform.rotation);//as GameObject;
Destroy(gameObject, 2);
explosionSound.Play();
}
}
这是第二个脚本
public class ObjectDamage : MonoBehaviour {
[SerializeField] int startingHealth = 1000;// starting health of the car
private int currentHealth;// current health of car
//Class and componant assigns
private ObjectExplode objectExplode;
void Start()
{
currentHealth = startingHealth;// start the level with ful health
//Class and componant setups
objectExplode = FindObjectOfType<ObjectExplode>();
}
void Death()
{
gameObject.SetActive(false);// destroy the car
}
public void Hurt(int damageToTake)
{
currentHealth -= damageToTake;
if (currentHealth <= 0)
{
Death();
objectExplode.Explode();
}
}
}
更正感兴趣的代码
public class ObjectDamage : MonoBehaviour {
[SerializeField] int startingHealth = 1000;// starting health of the object
private int currentHealth;// current health of object
private ObjectExplode objectExplode;
void Start()
{
currentHealth = startingHealth;// start the level with ful health
objectExplode = gameObject.GetComponent<ObjectExplode>();
}
void Death()
{
gameObject.SetActive(false);
}
public void Hurt(int damageToTake)
{
currentHealth -= damageToTake;
if (currentHealth <= 0)
{
Death();
objectExplode.Explode();
}
}
}
第二个代码
public class ObjectExplode : MonoBehaviour
{
// This goes on objects that will be destroyed
[SerializeField] AudioSource explosionSound;
[SerializeField] GameObject explosionPrefab;// Set the explosion particle
[HideInInspector] public bool explode = false;// make the object explode
private SphereCollider sphereCollider;
void Start()
{
sphereCollider = GetComponent<SphereCollider>();
sphereCollider.enabled = false;
}
public void Explode()
{
sphereCollider.enabled = true;
GameObject explosionClone = Instantiate(explosionPrefab, gameObject.GetComponent<Rigidbody>().transform.position,gameObject.GetComponent<Rigidbody>().transform.rotation);
Destroy(explosionClone, 2);
explosionSound.Play();
}
}
并将此代码添加到您想要造成伤害的角色
public class DamageToGive : MonoBehaviour
{
private int damage = 0;
public int damageToGive;
public string getTag = "";
void OnTriggerEnter(Collider other)
{
if (other.tag == getTag && damage == 0)
{
if (getTag == "TAGGOESHERE")
{
other.GetComponent<ZombieHealth>().Hurt(damageToGive);
}
damage++;
}
}
Void OnTriggerExit(Collider other)
{
if(damage > 0)
{
damage = 0;
}
}
}
答案 0 :(得分:1)
当然,它会炸毁你放入场景的第一个。这就是FindObjectOfType()
的作用。它在场景层次结构中找到具有指定类型的第一个对象。
说明强>
返回Type type的第一个活动加载对象。
您希望炸毁一辆特定的汽车,以获取该特定汽车的参考资料。
看起来您的第二个脚本附加到第一个脚本附加到的同一个对象,因此您实际需要的是gameObject.GetComponent<ObjectExplode>()
。