Error calling method from one script to another in Unity - c#

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!

Related

I was getting 3 errors when following a tutorial and I don't know how to fix them

These are my 3 errors that I have:
Assets/Scripts/Controllers/PlayerController.cs(13,27): error CS0115: 'PlayerController.RetrieveMoveInput()': no suitable method found to override
Assets/Scripts/Controllers/PlayerController.cs(8,26): error CS0115: 'PlayerController.RetrieveJumpInput()': no suitable method found to override
Assets/Scripts/Controllers/PlayerController.cs(6,33): error CS0246: The type or namespace name 'InputController' could not be found (are you missing a using directive or an assembly reference?)
This was the video I was watching: https://www.youtube.com/watch?v=lcw6nuc2uaU&t=140s
These are my 2 scripts:
InputController:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Input : ScriptableObject
{
public abstract float RetrieveMoveInput();
public abstract bool RetrieveJumpInput();
}
PlayerController:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "PlayerController", menuName = "InputController/PlayerController")]
public class PlayerController : InputController
{
public override bool RetrieveJumpInput()
{
return Input.GetButtonDown("Jump");
}
public override float RetrieveMoveInput()
{
return Input.GetAxisRaw("Horizontal");
}
}
Sorry if I've not formatted this correctly, haven't been on here in so long!
You've made a typo depending on how you want to view it should be either:
//wrong class declaration
public abstract class Input : ScriptableObject
{...}
//correct class declaration (how it is written in the github of the tutorial)
public abstract class InputController : ScriptableObject
{...}
alternatively you could also do
//corrected class declaration in other script
public class PlayerController : Input
However I would advise against that and use the tutorial variant, because Input is already a default Unity class that is used for input devices and you'll just be confusing yourself and the compiler.

Unity script error (The script don't inherit native class that can manage a script error )

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.

C# class can't see static method of another class

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");
}

Am scripting using C# in Unity but cant run code

I'm getting started with scripting in Unity and after running the attached codes. i get the error in the unity console. Any assistance for me.
Error in unity image.
This the the code i run in the VS for the unity.
enter code here
**using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Start method is Called");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update is calling");
}
}**
you have 2 Classes named CubeScript in your project. its the reason of error
Look at other script if one contains the same name of class
to avoid duplicate classes, you could add namespace too:
namespace NameOfSceneForExample
{
public class CubeScript : MonoBehaviour
{
}
}

Access public static variable from another script Unity

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.

Categories

Resources