KillPlayer, Null Reference Exception.
Hey there. I've been following a tutorial on how to make a basic platformer. I have all the right scripts, but it simply wont work. Here are the scirpts that I tested o my friends game, and suprisingly there it worked.`using UnityEngine; using System.Collections;
public class LevelManager : MonoBehaviour {
public GameObject currentCheckpoint;
private PlayerController player;
// Use this for initialization
void Start () {
player = FindObjectOfType<PlayerController>();
}
// Update is called once per frame
void Update () {
}
public void RespawnPlayer()
{
Debug.Log ("Player Respawns");
player.transform.position = currentCheckpoint.transform.position;
}
using UnityEngine; using System.Collections;
public class KillPlayer : MonoBehaviour {
public LevelManager levelManager;
// Use this for initialization
void Start () {
levelManager = FindObjectOfType<LevelManager>();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.name == "Bohater_0")
{
levelManager.RespawnPlayer();
}
}
} My scene is basically just player, some platform and spikes. The script are linked to the right object, Kill player to spikes, Level Manager to a Level Manager, and i know that i works, because i get the message "Player Respawns". However the transform position doesnt work, and i get the error. I also set the current checkpoint in the Level Manager. Anyone know how to fix this? If you do then please help me!
Answer by Zoogyburger · Feb 11, 2016 at 09:33 PM
You need to make sure that your player is named exactly "Bohater_0". When you use OnTriggerEnter2D both the Player and the spikes need to have a Rigidbody2D. if this doesn't solve the problem post your PlayerController script and tell me the exact error.
P.S. If you're new I suggest reading the FAQ and user guide:)
The name is Bohater_0, and spikes disappeared after adding Rigidbody 2D. What now?
ALso i still have a error with The referenced script on this Behaviour ( Game Object "Bohater_0") is missing, but PlayerController does work.
Oh, on box collider he had Is trigger on cus it work work without but, and after turning it off, the spikes appeared, and script not working!
using UnityEngine;
using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour {
private Rigidbody2D m_Rigidbody;
public float m_Speed ;
public float m_JumpHeight ;
public Transform groundCheck;
public float groundCheckRadius;
public Layer$$anonymous$$ask whatIsGround;
private bool grounded;
private bool doubleJumped;
// Use this for initialization
void Start () {
m_Rigidbody = GetComponent<Rigidbody2D>();
}
void FixedUpdate(){
grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
}
// Update is called once per frame
void Update () {
if (grounded)
doubleJumped = false;
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)&& grounded) {
m_Rigidbody.velocity = new Vector2 (m_Rigidbody.velocity.x, m_JumpHeight);
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) && !doubleJumped && !grounded) {
m_Rigidbody.velocity = new Vector2 (m_Rigidbody.velocity.x, m_JumpHeight);
doubleJumped = true;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.D)) {
m_Rigidbody.velocity = new Vector2 (m_Speed, m_Rigidbody.velocity.y);
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A)) {
m_Rigidbody.velocity = new Vector2 (-m_Speed, m_Rigidbody.velocity.y);
}
}
}
I made your game and did not modify any script and it works for me. $$anonymous$$aybe you should start over.
I created an empty Gameobject and called it Bohater_0 and gave him the following components: Sprite Renderer, Player Controller(Script), Rigidbody2D, BoxColider2D
I created an empty Gameobject and called it Spikes and gave it the following components: Sprite Renderer, $$anonymous$$ill Player(Script), Rigidbody2D,BoxColider2D
I created an empty Gameobject and called it Level$$anonymous$$anager and gave it the Level $$anonymous$$anager(Script)
I created an empty Gameobject and called it Checkpoint and gave it a Sprite Renderer
I created an empty Gameobject and called it Ground.
On Bohater_0, I set Speed to be 5, Jump Height to be 5, dragged Ground into the Ground Check, Ground Check Radius to be 1, and What is Ground to be Nothing. In the Rigidbody Gravity Scale to be zero, Box Collider to be IsTrigger
On Spikes, dragged my Level$$anonymous$$anager gameobject on to the Level $$anonymous$$anager slot in the $$anonymous$$illPlayer Script. In the Rigidbody Gravity Scale to be zero, Box Collider to be IsTrigger
On Level$$anonymous$$anager, dragged my Checkpoint onto Current Checkpoint If this STILL doesn't work, you're file is probably corrupted.