有人能找出这个问题吗
unity3d吧
全部回复
仅看楼主
level 1
五関 楼主
NullReferenceException: Object reference not set to an instance of an object
Player.Control () (at Assets/Player.cs:94)
Player.Update () (at Assets/Player.cs:86)
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public Transform m_transform;
CharacterController m_ch;
public float m_movSpeed;
public int m_life=5;
float m_gravity=2.0f;
Transform m_muzzlepoint;//枪口的位置
public LayerMask m_layer;//射击时,射线能射到的碰撞层;
public Transform m_Fx;//射中目标后的粒子效果
public AudioClip m_audio;//射击音效
float m_shootTimer=0;
Transform m_camTransform;
Vector3 m_camRot;
float m_camHeight=1.4f;
void Start () {
//武器模型枪口有一个muzzlepoint 的空游戏体,层级之间要用/分隔
m_muzzlepoint = m_camTransform .FindChild ("M16/weapon/muzzlepoint").transform;
m_transform = this.transform;
m_ch = this.GetComponent<CharacterController>(); //获取组件
m_camTransform = Camera.main.transform;//获取MainCamera摄像机
Vector3 pos = m_transform .position;
pos.y += m_camHeight ;
m_camTransform .position = pos;
m_camTransform.rotation = m_transform .rotation;
m_camRot = m_camTransform .eulerAngles;//欧拉角
}
public void OnDamage(int damage){
m_life -= damage;
//更新UI
GameObject.Find("GameManager").GetComponent<GameManager>().SetLife(m_life);
}
// Update is called once per frame
void Update () {
//更新射击间隔时间
m_shootTimer -= Time.deltaTime;
//鼠标左键射击
if (Input .GetMouseButton (0) && m_shootTimer <= 0) {
m_shootTimer =0.1f;
//射击音效
//减少弹药,更新弹药UI
GameManager .Instance.SetAmmo(1);
//RaycastHit 用来保存射线的探测结果
RaycastHit info;
//从muzzlepoint的位置,向摄像机面向的正方向射出一根射线
//射线只能与m-player所指定的层碰撞;
bool hit =Physics .Raycast (m_muzzlepoint .position ,m_camTransform .TransformDirection
(Vector3 .forward ),out info ,100,m_layer );
if (hit)
{
//如果射中了TAg 为enemy的游戏体
if(info .transform .tag.CompareTo ("enemy")==0)
{
Enemy enemy=info .transform .GetComponent <Enemy >();
//敌人减少生命
enemy.OnDamage(1);
}
//在射中的地方释放一个粒子效果
Instantiate (m_Fx ,info .point ,info.transform .rotation );
}
}
if(m_life<=0)
return;
Control();
}
void Control(){
float rh = Input .GetAxis ("Mouse X");//获取鼠标移动距离;
float rv = Input .GetAxis ("Mouse Y");//鼠标在屏幕的x和y方向上,从上一帧到目前滑过的增量
m_camRot .x -= rv;
m_camRot .y += rh;
m_camTransform .eulerAngles = m_camRot;
Vector3 camrot = m_camTransform .eulerAngles;
camrot .x = 0;
camrot .z = 0;
m_transform .eulerAngles = camrot;//使主角的面向方向与摄像机一致;
float xm = 0, ym = 0, zm= 0;
//重力运动
ym -= m_gravity * Time .deltaTime;
if (Input.GetKey (KeyCode.W)) {
zm += m_movSpeed * Time .deltaTime;
}
else if (Input.GetKey (KeyCode.S)) {
zm -= m_movSpeed * Time .deltaTime;
}
if (Input.GetKey (KeyCode.A)) {
xm -= m_movSpeed * Time .deltaTime;
} else if (Input.GetKey (KeyCode.D)) {
xm += m_movSpeed * Time .deltaTime;
}
m_ch .Move (m_transform .TransformDirection (new Vector3 (xm, ym, zm)));
Vector3 pos = m_transform .position;
pos .y +=m_camHeight;
m_camTransform .position = pos;
}
void OnDrawGizmos(){
Gizmos .DrawIcon (this .transform .position ,"Spawn.tif");
}
}
2015年12月12日 08点12分 1
level 7
你没拖动物体实例化吧
2015年12月12日 08点12分 2
level 1
遇到了同样问题 楼主知道怎么解决吗 急求
2016年12月31日 05点12分 4
level 1
哈哈我刚学这个, 这个问题是需要把enemyspawn设置为prefabs,然后把enemy模型指定给enemyspawn
2017年07月10日 03点07分 5
能说详细点吗
2023年05月29日 07点05分
1