Hi. I need help to fix this C# script.
Error = NullReferenceException: Object reference not set to an instance of an object
PlayerCollision.OnCollisionEnter (UnityEngine.Collision collisionInfo) (at Assets/Scripts/PlayerCollision.cs:17)
Hi. I need help to fix this C# script.
Error = NullReferenceException: Object reference not set to an instance of an object
PlayerCollision.OnCollisionEnter (UnityEngine.Collision collisionInfo) (at Assets/Scripts/PlayerCollision.cs:17)
Sry for my bad English i comming from Denmark
I can forgive you for your english but I’ll never forgive you for that ambiguous title.
Looks like you can’t find the Gameovertext component. Have you double checked that you have it in inspector?
Yes i have.
but is line 15 not 17
You pasted 17 in OP.
Anyway GetComponent will only work if all of those
GetComponent<Highscoresaver>().HighScore();
GetComponent<PlayerMovement>().enabled = false;
GetComponent<Gameovertext>().Gameover();
GetComponent<Taptext>().Texttap();
GetComponent<Generator>().enabled = false;
are compoments of (presumably) Player GameObject.
But are they?
If not, you’ll have to point to the GameObject that houses those components and access them that way.
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PlayerCollision : MonoBehaviour
{
public PlayerMovement movement;
public Text gameover;
public Highscoresaver highscore;
private void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Barrier")
{
GetComponent<Highscoresaver>().HighScore(); //this works becouse you have a Highscoresaver script on this gameObject
GetComponent<PlayerMovement>().enabled = false; //this works becouse you have a PlayerMovement script on this gameObject
GetComponent<Gameovertext>().Gameover(); // there is no Gameovertext script on this gameObject. you might need to do GameObject.FindWithTag("name of the object").GetComponent<Gameovertext>().Gameover();
GetComponent<Taptext>().Texttap(); // this might fail too. GameObject.FindWithTag("name of the object").GetComponent<Taptext>().Texttap();
GetComponent<Generator>().enabled = false; // this might fail too. GameObject.FindWithTag("name of the object").GetComponent<Generator>().enabled = false;
Debug.Log("RIP");
}
}
}