为了账号安全,请及时绑定邮箱和手机立即绑定

提示Object reference not set to an instance of an object。未将对象引用设置到对象的实例 怎么搞啊

using UnityEngine;

using System.Collections;


public class Player : MonoBehaviour

{

public  GameObject   mClickEffectPrefab;

public  GameObject   mClickEffect;

public float walkSpeed = 2f;

public Light light;

public MovementMotor movement;

private Vector3[] mPath;

private bool bWalking = false;

private int mPathIndex = 0;


// Use this for initialization

void Start ()

{

GameObject.Find ("Main Camera").GetComponent<SceneCamera> ().player = this.gameObject;

}

// Update is called once per frame

void Update ()

{

// Debug.Log ("Horizontal value: " + Input.GetAxis ("Horizontal"));

Vector3 direction = Input.GetAxis ("Horizontal") * transform.right + 

Input.GetAxis ("Vertical") * transform.forward;

//movement.movementDirection = direction.normalized;


// GetComponent<Rigidbody> ().AddForce (direction.normalized * walkSpeed, ForceMode.Acceleration);


transform.position = transform.position + 

walkSpeed * direction * Time.deltaTime ;

WalkForward ();

return;


if (bWalking) {

bWalking = Walking ();

} else {

}

}


public void WalkTo (Vector3 pos, bool withClickEffect = true)

{


if ((this.transform.position - pos).magnitude < 0.001f) {

return;

}


Vector3[] corners = CalcPathCorners (this.transform.position, pos);


if (corners.Length >= 2) {

bWalking = true;

mPath = corners;

bWalking = true;

// Animation anim = GetComponent<Animation> ();

// anim.CrossFade ("run_forward", 0.3f);

mPathIndex = 0;

transform.position = mPath [0];

if (mClickEffect != null && withClickEffect) {

mClickEffect.SetActive (true);

pos.y += 0.5f;

mClickEffect.transform.position = pos;

Invoke ("HideClickEffect", 1.5f);

}

}

}


private void HideClickEffect ()

{

if (mClickEffect != null) {

mClickEffect.SetActive (false);

}

}


private bool Walking ()

{

bool re = true;


Vector3 curSegSrc = mPath [mPathIndex];

Vector3 curSegDst = mPath [mPathIndex + 1];

Vector3 curPos = transform.position;


Vector3 normalDir = (curSegDst - curSegSrc);

normalDir.Normalize ();


curPos += (normalDir * walkSpeed * Time.deltaTime);


float over = (curPos - curSegSrc).magnitude - (curSegDst - curSegSrc).magnitude;

if (over > 0) {

mPathIndex ++;

if (mPathIndex > mPath.Length - 2) {

curPos = mPath [mPath.Length - 1];

transform.position = curPos;

re = false;

Animation anim = GetComponent<Animation> ();

// anim.CrossFade ("idle", 0.3f);

} else {

curSegSrc = mPath [mPathIndex];

curSegDst = mPath [mPathIndex + 1];

normalDir = (curSegDst - curSegSrc);

normalDir.Normalize ();

curPos = curSegSrc + normalDir * over;


transform.position = curPos;

}

} else {

transform.position = curPos;

}

return re;

}



Vector3[] CalcPathCorners (Vector3 src, Vector3 dst)

{

Vector3[] corners = null;

NavMeshPath path = new NavMeshPath ();

//if (NavMesh.CalculatePath(src, dst, -1, path)){

if (false) {

corners = path.corners;

} else {

corners = new Vector3[2]{src, dst};

}

return corners;

}


void WalkForward()

{

transform.position = transform.position + walkSpeed * transform.forward * Time.deltaTime ;

}

}


正在回答

4 回答

不好意思,看错了。这个是error。

如果已在Inspector中设置了Camera的Player,可void Start ()中的效果相同的

“GameObject.Find ("Main Camera").GetComponent<SceneCamera> ().player = this.gameObject;”可以直接删除掉。

如果想在脚本中设置Camera跟随Player的效果,亲测了两个方法:

(1)将“GameObject.Find ("Main Camera").GetComponent<SceneCamera> ().player = this.gameObject;”移至void Awake()方法中

(2)将“GameObject.Find ("Main Camera").GetComponent<SceneCamera> ().player = this.gameObject;”分解成

“GameObject MainCamera;

MainCamera=GameObject.Find("Main Camera");

MainCamera.getComponent<SceneCamera>().player=this.gameObject;

个人推测原因:某处用到了还未来得及初始化的对象,与实现函数的时间顺序有关。


方法原理:

(1)Awake()是场景启动时,MonoBehavior创建后,立刻调用的函数,所以不会产生来不及设置实例对象的情

况。

(2)= =咳咳,不知道为什么这样就行地通了,麻烦知道的人告知一下。


若解释的不对,希望能指出。谢谢


0 回复 有任何疑惑可以回复我~

这个只是因程序未完善出现的Warning,不影响运行,可以忽略

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

提示Object reference not set to an instance of an object。未将对象引用设置到对象的实例 怎么搞啊

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信