I'm writing a code in unity where the player clicks on a mousetrap, which sets the boolean mousetrap = true and increases its value to 1. That is attached to a gameobject called Trap. When I click on another gameobject in another scene, I want it to transition to another scene.
Collection of items (Collect.cs):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
public class MouseCollect : MonoBehaviour
{
public int trap = 0;
public bool mousetrap;
void Start()
{
mousetrap = false;
}
public void OnMouseDown()
{
if(gameObject.tag == "Trap")
{
trap++;
mousetrap = true;
GetComponent<BoxCollider2D>().enabled = false;
}
}
void Update()
{
}
}
Script I want to have it referenced in (Mouse.cs)
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Mouse : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//GameObject.Find("Trap").GetComponent<MouseCollect>().mousetrap;
if (GameObject.Find("Trap").GetComponent<MouseCollect>().mousetrap && gameObject.tag == "MouseArea")
{
SceneManager.LoadScene(10);
}
}
void Update()
{
}
}
It doesn't work and it gives me nullreferenceexception. I'm pretty new to Unity and C# so any help is appreciated!