I've run into some accessibility problem with one of my Unity projects. I created a class that does NOT inherit from MonoBehaviour. It has a method public static void LoadScene(string sceneName).
When I try to call this method from another class, I get a syntax error. This is my first script with the static method:
public class GameLoader
{
public static void LoadScene(string sceneName)
{
SceneManager.LoadSceneAsync(sceneName);
}
}
And here is my other script:
public class GameHandler : MonoBehaviour
{
private void Start()
{
GameLoader.LoadScene("MyScene"); //Syntax error
}
}
Normally, I would have some idea about what might the problem be, but in this case, the GameHandler recognizes GameLoader as class, but after the dot (GameLoader.), it does not find any property or function at all. And I get a syntax error when I try to write anything after the dot.
I experimented a little and it doesn't seem like I would cross another class with the name GameLoader and the neccessary namespace was added as well.
I'm pretty lost here, I hope someone can help me out.
Original codeGameLoader:
using UnityEngine.SceneManagement;
using UnityEngine;
namespace MyGame
{
namespace System
{
public class GameLoader
{
public static void LoadScene()
{
}
}
}
}
Original UIHandler:
using UnityEngine;
using System;
namespace MyGame
{
namespace System
{
namespace UI
{
public class UIHandlerMenu : MonoBehaviour
{
GameLoader.LoadScene();
}
}
}
}
Error message:
Severity Code Description Project File Line Suppression State
Error IDE1007 The name 'GameLoader.LoadScene' does not exist in the current context.
And the same error for just LoadScene itself.
Edit:
After your edit it is now obvious where the problem lies.
Try moving your call GameLoader.LoadScene("bla"); into a method.
If you want this method getting called when instantiating your handler you can move it to a constructor.
Example:
public UiHandlerMenu() {
GameLoader.LoadScene("bla");
}
Related
i'm currently making a quiz game using unity with guidance from youtube( this is the video that i follow https://www.youtube.com/watch?v=G9QDFB2RQGA). now i'm stuck at the when i tried to add the answer script into buttons component (14:30). everytime i tried, the error message the script don't inherit native class pop out. have been a day trying to find solution and i still can't find it. tried to change the script file name, class name to be identical, etc, but still cant add the script. there's no compiler error tho. anyone know whats wrong?
Answers.cs
using System.Collections.Generic;
using UnityEngine;
public class Answers : MonoBehaviour
{
public bool isCorrect = false;
public Quizmanager quizmanager;
public void Answers()
{
if (isCorrect)
{
Debug.log("Correct answer!");
else
{
Debug.log("Wrong answer ~");
}
}
}
Quizmanager.cs
using System.Collections.Generic;
using UnityEngine;
public class Quizmanager : MonoBehaviour
{
public List<Questions> QnA;
public GameObject[] options;
public int currentquestions;
public Text Questiontxt;
private void Start()
{
generatequestion();
}
public void correct()
{
QnA.RemoveAt[currentquestions];
generatequestion();
}
void Setanswers()
{
for(inr 1=0; 1 <options.Length; int++)
{
options[i].GetComponent<AnswersScript>().isCorrect = false;
options[i].transform.GetChild(0).GetComponent<Text>().text = QnA[currentquestions].Answers[i];
if(QnA[currentquestions].CorrectAnswer == i + 1)
{
options[i].GetComponent<AnswerScript>().isCorrect = true;
}
}
}
void generatequestion()
{
currentquestions = Random.Range(0, QnA.Count);
Questiontxt.text = QnA[currentquestions].Question;
Setanswers();
}
}
I've had similar issues while using visual studio. The only way I managed to solve them was by creating a new script, and copying the class content (not the class name) into the new file and deleting the old file. The class name might change and the solution is a bit anticlimactic but it usually saves me from this issue.
I started playing games for the first time in Unity. I get the error in the title. Why could it be? Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.SceneManagement;
public class Quıt : MonoBehaviour
{
{
Application.Quıt();
}
}
It looks like you just need to put it in a method. Also Application.Quit() does not work in the editor.
public class Quit : MonoBehaviour {
public void QuitApplication() {
Application.Quit();
}
}
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods
https://docs.unity3d.com/Manual/MobileOptimizationScriptingMethods.html
1st script:
public class VRTK_VRInputModule : PointerInputModule
Method in in:
public virtual void Click(VRTK_UIPointer pointer, List<RaycastResult> results)
2nd script:
public class ARHeadWebCamTextureExample : MonoBehaviour
Im trying to call Click from the 1st script by using:
public VRTK_VRInputModule vrtk;
vrtk = new VRTK_VRInputModule();
vrtk.Click();
Im getting an error: The type or namespace name `VRTK_VRInputModule' could not be found. Are you missing an assembly reference?
What am I doing wrong??
EDIT:
I created a "using VRTK" but got another error:
using OpenCVForUnity;
using DlibFaceLandmarkDetector;
using VRTK;
namespace DlibFaceLandmarkDetectorExample {
public class ARHeadWebCamTextureExample : MonoBehaviour
{
public VRTK_VRInputModule vrtk;
public void NeededFunction()
{
vrtk = new VRTK_VRInputModule();
vrtk.Click();
}
}
}
When hoovering over Click I get the following error:
Error
Thank you for your help!
I am trying to access and change of the value of a variable from another script, I have watched various different tutorial on youtube but cannot seems to do it. It always come up with an error as following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GlobalValues : MonoBehaviour
{
public static bool c2u;
}
And the other script
public class Choosing : MonoBehaviour {
private void Start()
{
GlobalValues.c2u = false;
}
}
In the "Choosing" script, the GlobalValues always shows "'GlobalValues' does not exist in this current context". What can i do to fix this problem
That code should work fine. The only way I could see it not working is if you haven't saved one of the scripts.
Using Unity 5.4 beta with Hololens, setting a class within a namespace disables some UnityEngine functionality.
If I declare a class, it all works fine. If I wrap it inside a namespace, classes don't see each other, even within the same namespace or with the using addition.
Making a class MonoBehaviour, I can drag and drop it onto a game object, if I wrap that class inside a namespace, Unity complains that it is not a MB or it has issue so it cannot be dragged.
Does anyone have similar issue? Just asking before reporting bugs since 5.4 is still beta.
Classes don't see each other because you are not importing them or accessing them through their namespace. To access anything in a namespace, you must import the namespace or call the namespace followed by the class name. Check below for both examples.
Class with namespace:
namespace MyANameSpace
{
public class A
{
}
}
In order to see class A, you have to import it with the using keyword.
using MyANameSpace;
public class B : MonoBehaviour
{
A a;
// Use this for initialization
void Start()
{
a = new A();
}
}
Another method is to access them directly through the namespace.
public class B : MonoBehaviour
{
MyANameSpace.A a;
// Use this for initialization
void Start()
{
a = new MyANameSpace.A();
}
}
If this does not solve your problem, then you have to post your code. That's likely not a bug.
I managed to figure out what I think is happening. Here is what I had:
namespace Company.Hololens
{
public enum GazeState
{
None = -1, NoHit, Hit
}
public class CursorEventArg : EventArgs
{
}
public class CursorController : Singleton<CursorController>
{
}
}
and it seems as if Unity does not like the order of class declaration. Pushing the EventArg down to the end of the script and it goes fine.
Not sure if this should be considered a bug, I have never seen any mention of class declaration order. Declaring an interface on top is fine though.