0

My Problem is the following:

I´m trying to code a script for Unity with which I can change the scene when one specific Object was clicked. But I get the following error:

NewBehaviourScript.cs(19,21): error CS0176: Member 'SceneManager.LoadScene(string)' cannot be accessed with an instance reference; qualify it with a type name instead

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class NewBehaviourScript : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform.name == "Cube")
                {
                    SceneManager mySceneManager = new SceneManager();
                    mySceneManager.LoadScene("SceneTwo");
                }

            }
        }
    }
}
Jusses
  • 1
  • 1

1 Answers1

0

SceneManager.LoadScene is a static method. This means it belongs to a type SceneManager, not some instance of SceneManager. This means you don't have to create new instance of SceneManager and instead of

SceneManager mySceneManager = new SceneManager();
mySceneManager.LoadScene("SceneTwo");

just use

SceneManager.LoadScene("SceneTwo");

You can read more about static members in c# in Microsoft docs. Also next time first look for examples in Unity's documentation, it can save you some time.

Kacper
  • 520
  • 5
  • 20