我在Unity3D工作,我收到错误
你能帮帮我吗?谢谢:))NullReferenceException:对象引用未设置为对象的实例PlayerMoveScript.OnTriggerEnter2D
PlayerMoveScript:
using UnityEngine;
using System.Collections;
public class PlayerMoveScript : MonoBehaviour {
public float speed;
BarrMoveScript barrMoveScript;
// Use this for initialization
void Start () {
barrMoveScript = GetComponent<BarrMoveScript>();
}
// Update is called once per frame
void Update () {
Vector3 pos = transform.position;
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
pos.x -= speed;
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
pos.x += speed;
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
pos.y += speed;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
pos.y -= speed;
}
transform.position = pos;
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("con_col"))
{
print("con col");
return;
}
else if(col.CompareTag("barr1"))
{
print("hi, i collide");
barrMoveScript.IsInside(true);
}
}
}
BarrMoveScript:
using UnityEngine;
using System.Collections;
public class BarrMoveScript : MonoBehaviour {
public GameObject player;
bool inside = false;
// Use this for initialization
void Start () {
}
public bool IsInside(bool insi)
{
if (insi)
inside = true;
return inside;
}
// Update is called once per frame
void Update () {
Vector3 pos = transform.position;
if (Input.GetKeyDown(KeyCode.LeftArrow))
inside = false;
if (inside)
{
pos.x = player.transform.position.x;
pos.y = player.transform.position.y;
transform.position = pos;
}
}
}