Hey guys I feel like Ive tried everything here and I cant get this error to go away. Im making a 2D platformer and would like to add some spikes to the game. If the player collides with them, they "die" and respawn at the beginning of the map. I placed the spike sprite in the game and attached my "Hazard" script to it (no debugging errors what so ever). The spike "Is Triggered" when the player (who has the tag "Player") walks into it. I created an empty game object called "start" and placed it at the beginning of the map. This is where the player will transfer to when they collide with the spike. I drag the "start" object and attach it to the Hazard Script so that they are linked together.
When I press play, the player moves fine and as soon as it touches the spike, it flashes the error "NullReferenceException: Object reference not set to an instance of an object". I reference the start object though so I dont understand what it means by it not being set?! Is there something else that it is having difficulty with?
Here is the Hazards Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hazard : MonoBehaviour {
private Controls player;
public Transform start;
void Start()
{
player = FindObjectOfType();
}
void Update()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
player.transform.position = start.position;
}
}
}
![Here I have the spike selected. You can see at the bottom of the inspector, the Hazard script is attached to it as well as "start". The "start" game object is located at the bottom of my hierarchy (no matter what object I put in there, it throws the same error). ][1]
![Lastly, here is the inspector of my Player, idk if it can help at all. There is also a sprite rendered and animation in its inspector but I couldnt fit it in the snippet. The "Player" tag is set to him.][2]
Seriously, any help would be amazing. I feel like ive tried so many things and I cant get it to work. Thanks guys!
[1]: /storage/temp/96389-capture.jpg
[2]: /storage/temp/96390-capture.jpg
↧