I am adding the following line of code in my script to enable controller support for unity VR game which is being played in google daydream. Without this code, the controller is not showing in the final build of the game
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class InputManager : MonoBehaviour
{
public GameObject controllerMain;
public GameObject controllerPointer;
private void Start()
{
controllerMain.SetActive(true);
controllerPointer.SetActive(true);
GvrPointerInputModule.Pointer =
controllerPointer.GetComponentInChildren<GvrLaserPointer>();//3
}
}
Can someone explain me why I need to enable controllermain and controller during gamelay they are not disabled in the hierarchy.
And explain the 3rd line of code
The controllerMain and controllerPointer variables are just references to GvrControllerMain and GvrControllerPointer GameObject prefabs respectively. GvrControllerPointer provides built-in laser pointer used for raycasting. GvrControllerMain is required to use the Daydream controller.
Calling controllerMain.SetActive(true) will activate GvrControllerMain. The-same thing applies to GvrControllerPointer when you call call SetActive on it. Both of these should be activated when running daydream. This is why the activating and de-activating of them happens during run-time because that's when you can determine if this is a daydream device or not.
GvrPointerInputModule.Pointer is used to set the pointer type. There is a GvrLaserPointer and GvrReticlePointer. In your example, you were telling the Gvr module to use GvrLaserPointer and is a laser visual that helps users locate their cursor when its not directly in their field of view.
Related
This is an image of the errors
This is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManager;
public class LevelComplete : MonoBehaviour
{
public void LoadNextLevel ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().BuildIndex + 1);
}
}
Please help me with the errors
I tried to debug the error by checking the script and UI animations that connect to the event that changes the scene. I also tried using SceneManager.LoadScene (name); but it did not work.
You have two issues here. First of all, you declared the class twice. It might be twice in the same file, or in different files. Second of all, you need to use UnityEngine.SceneManagement instead of SceneManager.
The simplest solution is to check for double declarations, and for the using, replace
using UnityEngine.SceneManager; with using UnityEngine.SceneManagement.
I am learning unity 3d game development and I was following a tutorial and wrote this code for a ball to apply force on its X-axis, but unity said all compile errors must be resolved before going in Playmode:
code:
using System.Numerics;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddConstantForce : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GetComponent<RigidBody>().velocity = new Vector3(2,0,0);
}
}
I have update unity version. tried various new sample scenes, and after this error pops it also gives the error with default empty void start and void update functions too.
please help
Without the error information, this is somewhat difficult to debug but I do see two errors with your code.
First, you have a typo on your Update function you are calling GetComponent<RigidBody> when the actual name of the class is Rigidbody see Unity's documentation for reference https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
There is also an ambiguous reference between Vector3 as both Systems.Numerics & UnityEngine defines that type. you want to stick with the UnityEngine definition. so just delete the first line.
So in the end this code should be working without compilation errors
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddConstantForce : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GetComponent<Rigidbody>().velocity = new Vector3(2,0,0);
}
}
Try define the getcomponet variable in the void update and then make a new line with the varible first then velocity thing or/and use += insted of =.
I don't know why my scene is not loading, can I get help?
When I press the button nothing happens.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class gamecontroller : MonoBehaviour
{
public Text points;
public void Start() {
points.text = Score.scoreval + " POINTS";
}
public void Restart() {
SceneManager.LoadScene("SampleScene");
Score.scoreval = 0;
}
public void Menu()
{
SceneManager.LoadScene("Menu");
Score.scoreval = 0;
}
}
Reason might be that you did not assign the scene in your build. This is an easy fix if this is the case. To check if it is, you can go to the Build Settings... under the File tab. Once in there, you should see the Scenes In Build category, in there you should also see your scene(s). If not, you will need to assign them by going to each scene and pressing Add Open Scenes. Once done, it should work properly from there.
Not sure if this is an issue, it has been a while since I have coded in C#. But, in my scene manager, I wrote the scenes name as such:
public void StartButton()
{
SceneManager.LoadScene(labScene);
}
Not sure if that matters, but I thought I might as well put that out there.
There seems to be a bug from Unity side as it has happened with me sometimes that whenever I'm trying to load the scene using the LoadScene method by passing the SceneName as the parameter, Unity throws up an error even when those scenes have been added in the Build Settings.
Removing the scenes from the build settings and adding them again is what worked for me. :)
Working on getting familiar with C# and unity development. Today I am working on getting a reference to a Text UI object in my script. The following code below produces this error:
NullReferenceException: Object reference not set to an instance of an object
handle.Awake () (at Assets/handle.cs:20)
Script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using playPORTAL.Profile;
using UnityEngine.UI;
public class handle : MonoBehaviour
{
public Text myText;
// Start is called before the first frame update
void Start()
{
}
void Awake()
{
myText.text = "#organickoala718" ;
}
// Update is called once per frame
void Update()
{
}
}
What needs improving to correctly get a reference to the Text UI element?
In general: The same way as with any other Component.
Either reference it via the Inspector or use GetComponent(Tutorial) or one of its variations.
So if that Text Component is attached to the same GameObject as your script then you could use GetComponent(API) to get the reference on runtime
private void Awake ()
{
if(!myText) myText = GetComponent<Text>();
myText.text = "#organickoala718" ;
}
Also checkout Controlling GameObjects with Components
Btw you should remove the empty methods Start and Update entirely. They are called by the Unity Engine as messages if they exist so the don't need to exist and only cause unnecessary overhead.
You need to either set the myText value of your handle instance from another script, or set it in the Unity Editor's Inspector window, when you've selected a GameObject that has your handle component added.
You need to drag the object you are referencing to from the Unity editor, in the scene, to the script itself.
First attach the script you made to a GameObject in the Unity Scene.
Then drag the "text" component to the Script you recently attached to the GameObject
That would solve the problem you have.
Another approach would be to declare a
public GameObject UITextElement;
Instead of a public text as you did.
Do the same as i wrote before and in the script write:
UITextElement.GetComponent().text = "Write your text here!";
User can place objects (prefabs) at runtime using hands/gaze.
Voice command ""Remove" should remove the current focussed (looked at) object.
I tried instantiating the objects and adding the Intractable script. But I am stuck add adding OnfocusEnter and OnfocusExit events at runtime.
Hooking up the events on the prefab wont work as the references are in the scene.
I worked this out over on GitHub and posting it here so we can remove it from the other sources.
I didn't tackle the voice input yet as I am not there yet on my own MRTK project.
This submission should cover this answer for MRTK under version RC1. This was a quick job just to show proof of concept - feel free to modify and go on with it but I won't be :)
For run-time placement you would just need to add a method to instantiate an object that contains all of the information I setup in this example. There were some other solutions in the GitHub channel, I've copied the links below (not sure how long they will be active). This example is assuming you have some sort of already default prefab with the MRTK interactable class part of it.
Other discussions on GitHub from Microsoft: https://github.com/microsoft/MixedRealityToolkit-Unity/issues/4456
Example Video is here: https://www.youtube.com/watch?v=47OExTOOuyU&feature=youtu.be
Example Unity Package is here: https://github.com/JShull/MRTKExamples
Based on #jShull answer I came up with a simple solution for what I needed. Since there is no global listener for focus events I basically made my own.
I also added an earlier discussion (before I posted the question here) with two Microsoft Developers of the Mixed Reality Toolkit which could help out of you are looking for more functionality: https://github.com/microsoft/MixedRealityToolkit-Unity/issues/4456
"Object" script that is a component of the object that needs to be removed or interacted with.
using Microsoft.MixedReality.Toolkit.Input;
using UnityEngine;
public class Object: MonoBehaviour, IMixedRealityFocusHandler
{
public GameManager _gameManager;
public void OnFocusEnter(FocusEventData eventData)
{
Debug.Log("Focus ON: " + gameObject);
_gameManager.SetFocussedObject(gameObject);
}
public void OnFocusExit(FocusEventData eventData)
{
Debug.Log("Focus OFF: " + gameObject);
_gameManager.ResetFocussedObject();
}
}
"GameManager" script functions that sets the focussedObject
public void SetFocussedObject(GameObject object)
{
focussedObject = object;
}
public void ResetFocussedObject()
{
focussedObject = null;
}
Remove Object function is connect to the "Remove" global speech command in the "Speech Input Handler" component. It just removes the "focussedObject" inside the GameManager.