Unity namespace error does not exist with SceneManager - c#

I am using unity SceneManager but it gives me a namespace error that it does not exist in
using UnityEngine;
using UnityEngine.SceneManagement;
public class ManageGame : MonoBehaviour
{
bool gameHasEnded = false;
public void GameOver()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("GAME OVER");
Restart();
}
}
void Restart ()
{
SceneManagement.LoadSceneMode(SceneManager.GetActiveScene().name);
}
}
I read the official documentation about SceneManger and i checked the using part and it was the same.
My version is 2019.3.14.
Why does this happen?

Could you post some more information about your script? Which method of SceneManager are you trying to call and how exactly does this code look like? What's your Unity version? What are your other 'using' statements?
Without having more details we can only guess: Both the 'UnityEditor' as well as the 'UnityEngine' namespace contain a SceneManagement namespace, which might be causing some confusion.
EDIT:
Thanks for updating the question with more information. I see two issues:
1.) You are trying to call a method on a namespace (SceneManagement is a namespace not a class). Instead you want to access the class SceneManager in the SceneManagement namespace.
2.) LoadSceneMode() is not a method in SceneManager. There's an enum with that name, but no method. You want to use the method LoadScene()
So the correct line would be:
SceneManager.LoadScene(SceneManager.GetActiveScene().name);

Related

I have tried using a public void function from a different script and error message CS1061 pops up

I am making a game in Unity with dialogue boxes and have one script made for putting the dialogue box on the screen and another to have the actual text put on the dialogue box.There is one function in the first script I have to use in the second to make it work. The problem however is that when I try to reference the first script it always occur an error.
The error message is:
Assets\scripts\dialogueHolder.cs(29,18): error CS1061: 'dialogueManager' does not contain a definition for 'ShowBox' and no accessible extension method 'ShowBox' accepting a first argument of type 'dialogueManager' could be found (are you missing a using directive or an assembly reference?)
First code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class dialogueManager : MonoBehaviour
{
public GameObject dialogueBox;
public Text dialogueText;
public bool activeDialogue;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (activeDialogue == true && Input.GetKeyDown(KeyCode.Return))
{
dialogueBox.SetActive(false);
activeDialogue = false;
}
}
public void ShowBox(string lines)
{
activeDialogue = true
dialogueBox.SetActive(true);
dialogueText.text = lines;
}
}
Second code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class dialogueHolder : MonoBehaviour
{
public string dialogue;
private dialogueManager dMan;
public GameObject Manager;
// Start is called before the first frame update
void Start()
{
dMan = Manager.GetComponent<dialogueManager>();
}
// Update is called once per frame
void Update()
{
}
void OnTriggerStay2D(Collider2D other)
{
if(other.gameObject.name=="Shadow")
{
if(Input.GetKeyDown(KeyCode.Return))
dMan.ShowBox(dialogue);
}
}
}
I can clearly see that the function ShowBox I am trying to access from the first script is the problem. But I can't figure out what to do with the code to make it work. I have tried different methods to try and reference the Class dialogueManager. For an example I have tried refrencing the actual gameobject in the game or the script alone but I can't seem to get it to work. If I remove the function ShowBox the code will run but I need the ShowBox function to actually make the dialogue boxes in my game to work so that is not a solution.
"activeDialogue = true" maybe you should put a ";" after this

c# script class in unity cannot be found despite no compile errors and names matching

I am attempting to amend a c# sharp script to an empty object but I am constantly given the "script class cannot be found" error, not allowing me to add the script to the object. every fix I have found for this issue includes either there being a compile error which is found in the console (my console is entirely empty) or that the names do not match (which they 100% do).
Here is my code below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class MenuController : MonoBehaviour
{
[Header("Levels to load")]
public string _newGameLevel;
private string levelToLoad;
[SerializeField] private GameObject noSavedGameDialogue = null;
public void newgameYes()
{
SceneManager.LoadScene(_newGameLevel);
}
public void loadgameYes()
{
if (PlayerPrefs.HasKey("Savedlevel"))
{
levelToLoad = PlayerPrefs.GetString("SavedLevel");
SceneManager.LoadScene(levelToLoad);
}
else
{
noSavedGameDialogue.SetActive(true);
}
}
public void QuitButton()
{
Application.Quit();
}
}
Possible reason: the name of the script is different from the class.
Sometimes unity can (in my experience) fail finding the script if the only difference is lower/upper case. When that happens I think I usually need to delete and add a new script.
Try adding a script with different name.
Also try edit->preferences->externaltools-> regenerate project files

Unity How to declare propertly Input Actions in Script CS0246

I was following a tutorial for scripting Input Actions in Unity
After finished coding what the tutorial told I've got this error in the declarations of Input System's stuff CS0246 The type or namespace name 'InputActions' could not be found
The code that causes the error is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class GameInput : MonoBehaviour
{
private InputActions InputActions;
private InputAction StartAction;
void Start() {
InputActions = new InputActions();
}
private void OnEnable() {
StartAction = InputActions.Start.Start;
StartAction.Enable();
InputActions.Start.Start.performed += StartGame;
InputActions.Start.Start.Enable();
}
public void StartGame(InputValue value) {
// CODE HERE
}
}
The error is caused by the declaration private InputActions InputActions;
I think it is by the variable type InputActions that may not exist.
But the tutorial also uses a type that doesen't exist.
How can I make Unity to recognize the InputActions type?
Typo: Unity does not have an InputActions Type, but it has a InputAction Type. See how you spelled the type different in your example. InputActions does not exist so you can't use it.
Change InputActions to InputAction.

The type or namespace name 'Input' does not exist in the namespace 'UnityEngine.Experimental'

I am using unity's new input system and getting this error, I tried a lot to fix it but can't find the problem. Please help me.
Error:
Assets\Player01.cs(4,32): error CS0234: The type or namespace name 'Input' does not exist in the namespace 'UnityEngine.Experimental' (are you missing an assembly reference?)
Full Script(C#):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.Input;
public class Player01 : MonoBehaviour
{
public InputMaster controls;
void Awake ()
{
controls.Player01.Shoot.performed += _ => Shoot();
}
void Shoot ()
{
Debug.Log("We shot the sherif!");
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
}
The problem is that there is no namespace of UnityEngine.Experimental.Input. But, it only says “'Input' does not exist in the namespace 'UnityEngine.Experimental'”. ‘Experimental’ does exist and ‘Input’ does not. However, ‘InputSystem’ does. And that is the one you are looking for, not ‘Input’. You should change the first line to this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.InputSystem;
public class Player01 : MonoBehaviour
...
Just in case, if the first solution did not work, Close vs studio / code if open, then go to Edit -> Project Settings -> Input System Manager. There will be only one option available. Click the button, save your scene. Open a script. This worked for me. A lot of tutorials do not mention this part.

Cannot write my code right, because of an error CS0111

I'm a beginner in programming, So i don't know how to fix this error.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZombieScript : MonoBehaviour
{
public Transform player;
public Transform zombie;
public GameObject zombieScript;
bool canActive = false;
void Start()
{
if (canActive == false) { zombieScript.SetActive(true); }
}
void Update()
{
UnityEngine.Debug.Log(Mathf.Abs(Vector2.Distance(player.transform.position, zombieScript.transform.position)));
if (Mathf.Abs(Vector2.Distance(player.transform.position, zombieScript.transform.position)) <= 10.00000 && canActive == false)
{
zombieScript.SetActive(true);
canActive = true;
}
}
}
Unity writes:
Assets\ZombieScript.cs(5,14): error CS0101: The namespace '' already contains a definition for 'ZombieScript'
Assets\ZombieScript.cs(12,10): error CS0111: Type 'ZombieScript'
already defines a member called 'Start' with the same parameter types
Assets\ZombieScript.cs(17,10): error CS0111: Type 'ZombieScript'
already defines a member called 'Update' with the same parameter types
in advance, thank u <3
A basic structure of a C# class looks like this:
using ...
namespace CompanyXYZ.ProductABC
{
public class Entity
{
// Constructors, properties, methods & etc.
...
}
}
CS1011 compiler error will be thrown whenever it found more than one definition within the same namespace.
Namespace allows us to avoid conflict of classes with the same name by grouping them in different namespaces.
In this case, you probably have more than one ZombieScript defined under the same namespace which is not shown in your code. Therefore, kindly check if you have it defined somewhere else (within the same namespace) and if found it you're good to go!

Categories