Hello I’m new to unity and I started off withe the Gmtk tutorial.
(I used godot before unity, so I’m not a complete beginner)
I get many error after I hit my restart button:
The Error: Nullreferenceexception,object reference not set an instance an object PipeMiddleScript.OnTriggerEnter2D. sc 23)
My code in the Pipemiddlescript
using Unity.VisualScripting;
using UnityEngine;
public class PipeMiddleTriggerScript : MonoBehaviour
{
public LogicManagerScript logic;
void Start()
{
logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicManagerScript>();
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.layer == 6)
{
logic.addScore(1);
}
}
}
And here is my code for restart the game:
public class LogicManagerScript : MonoBehaviour
{
public GameObject gameOverScreen;
public void gameOver()
{
gameOverScreen.SetActive(true);
}
public void restartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
The addScrore function didn’t work after i reload the scene, instead I print the error Nullreferenceexception.
And don’t understand why, because should be all objects destroy. Then it’s load the new Scene and every start function runs again ?
I think the problems lays somewhere in the line 10 in my pipemiddlescript, but I’m complete new to C#