NullReferenceException: Object reference not set to an instance of an object
UnityEngine.UIElements.UIR.RenderChain.Render (UnityEngine.Rect viewport, UnityEngine.Matrix4x4 projection, UnityEngine.UIElements.PanelClearFlags clearFlags) (at <ba477b09073148948adef3d3e1503d12>:0)
UnityEngine.UIElements.UIRRepaintUpdater.DrawChain (UnityEngine.Rect viewport, UnityEngine.Matrix4x4 projection) (at <ba477b09073148948adef3d3e1503d12>:0)
UnityEngine.UIElements.UIRRepaintUpdater.Update () (at <ba477b09073148948adef3d3e1503d12>:0)
UnityEngine.UIElements.VisualTreeUpdater.UpdateVisualTreePhase (UnityEngine.UIElements.VisualTreeUpdatePhase phase) (at <ba477b09073148948adef3d3e1503d12>:0)
UnityEngine.UIElements.Panel.UpdateForRepaint () (at <ba477b09073148948adef3d3e1503d12>:0)
UnityEngine.UIElements.Panel.Repaint (UnityEngine.Event e) (at <ba477b09073148948adef3d3e1503d12>:0)
UnityEngine.UIElements.UIElementsUtility.DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at <ba477b09073148948adef3d3e1503d12>:0)
UnityEngine.UIElements.UIElementsUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at <ba477b09073148948adef3d3e1503d12>:0)
UnityEngine.GUIUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at <70ac3a30392c42a48583a8f82077fee6>:0)
程式可以正常運行,但是不知道哪里出問題了,一直在報錯
百度后看到有人說是參考的實體物件未定義,剛學C#和unity,沒看出哪個沒被定義,救救孩子吧
附上使用的兩段腳本
腳本1:Playercontrol.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Playercontrol : MonoBehaviour
{
public float speed = 10f;
private int Maxhealth = 3;//最大生命值
private int Currenthealth=2;//當前生命值
public int Gamingmaxhealth { get { return Maxhealth; } }
public int Gaminghealth { get { return Currenthealth; } }
Rigidbody2D rbody;//定義剛體rbody
// Start is called before the first frame update
void Start()
{
rbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float moveX = Input.GetAxisRaw("Horizontal");//水平方向移動,A:-1 D:1 0
// 目前先禁用 float moveY = Input.GetAxisRaw("Vertical");//垂直方向移動,W:1,S:-1 0
Vector2 position = rbody.position;
position.x += moveX * speed * Time.deltaTime;
//禁 position.y += moveY * speed * Time.deltaTime;
rbody.MovePosition(position);
}
//改變玩家的生命值
public void Changehealth(int aomunt)
{
Currenthealth = Mathf.Clamp(Currenthealth + aomunt, 0, Maxhealth);
Debug.Log(Currenthealth+"/"+Maxhealth);
}
}
腳本2:Healthcollect.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//回血道具碰撞檢測
public class Healthcollect : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D other)
{
Playercontrol pc = other.GetComponent<Playercontrol>();
if (pc != null) { Debug.Log("玩家碰到櫻桃"); }
if (pc.Gaminghealth < pc.Gamingmaxhealth)
{
pc.Changehealth(1);
Destroy(this.gameObject);
}
}
}
uj5u.com熱心網友回復:
雙擊報錯看看是否指定了某句代碼uj5u.com熱心網友回復:
是在unity的控制臺報錯的,找不到指向uj5u.com熱心網友回復:
應該是你UI設定的問題你把UI禁了試試看轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/14059.html
標籤:Unity3D
