- Home /
Cannot use class in one script but other scripts can
I am currently running into an issue where for some reason I can use a specific class I made in basically all my other scripts except for one and I am trying to understand why I can't use it in that specific script.
Currently I created two empty game objects: GameController and UIController each with there own script (Scripts also share the same name). I am trying to pass the UIController class into GameController script so I can change some UI within the UIController script. However when I try to use UIController, I get a "type or namespace "UIController" cannot be found..." error.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour {
private GameObject respawnPoint;
private UIController uiController; ****Error Here****
[SerializeField] private int hitpoints = 1;
...
Does your UIController under any namespace ? Does it called UIController and not UiController ... Does the script name the same as the class name?
If Positive7's suggestions didn't help, we will need more details. Let's see the script in question.
We need more info can you post your UIController script please.
Answer by TypeFinalMagic · Sep 25, 2017 at 06:14 AM
After looking at the solution explorer in Visual Studio, I realized that my GameController script was not listed in the Asset folder in the explorer along with the other scripts. Then after more digging in Unity, I realized that the GameController script was misplaced in the scripts folder in the Standard Assets folder instead of the Scripts folder in the Asset folder containing all the folders and files. After moving the GameController script into the proper Scripts folder with all my other scripts, I was able to use the UIController script.
TLDR: Make sure that the script is placed in the same folder as the other scripts you are trying to use it with.
Answer by Raimi · Sep 23, 2017 at 04:16 PM
private UIController uiController;
void Start()
{
uiController = FindObjectOfType( UIController );
// Should only be 1 UIController script in scene
}
Not sure this is what you need, but hope it helps.
The problem is that I can't even put in the private UIController uiController;
line without visual studio throwing the "type or namespace "UIController" cannot be found..." error
Answer by Thaun_ · Sep 24, 2017 at 05:48 PM
Restarting Visual Studio makes it work for me, try that.
Restarting visual studio didn't help and neither did restarting the computer.
Have you created a C# script via Visual Studio or Unity? If from Visual Studio, delete it and recreate it in Unity.
Have you tried update your .NET? or update Unity Tools?
Can you check if your file is in the Unity Project Explorer?
Your answer
Follow this Question
Related Questions
Draw call minimizer 1 Answer
Help with script 1 Answer
[C#] Quaternion Rotations with Input.GetAxis problems. 1 Answer