0
我没看到这段代码有什么问题。它表示变量projectileEnemy没有分配给任何东西,即使我要通过检查器窗口分配给它预制件,但检查器窗口不会更新,因为有错误。我没有看到脚本实例化预制有什么问题
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Attack : MonoBehaviour {
public Transform playerPos = null;
private float playerDist;
public GameObject projectileEnemy = null;
private void Shoot()
{
GameObject projectileEnemy = Instantiate(projectileEnemy, transform.position, Quaternion.identity) as GameObject;
}
void Update() {
playerDist = playerPos.position.x - transform.position.x;
if (playerDist <= (3) && playerDist >= (-3))
{
Shoot();
if (playerDist < (0))
{
projectileEnemy.GetComponent<Rigidbody>().AddForce(transform.left * 10);
}
else
{
projectileEnemy.GetComponent<Rigidbody>().AddForce(transform.right * 10);
}
}
}
}