I'm trying to switch scenes when an object in a VR environment is pressed. Should be a simple line of code but when I try to execute it the game crashes. The game is build to the Oculus Go.
I know I've added the scenes to the build to that shouldn't be the problem. I also got the index of '1' right in the build settings.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SphereScript : MonoBehaviour
{
public void LoadScene()
{
SceneManager.LoadScene("1");
}
}
private void ProcessTouchpadDown()
{
if (!m_CurrentObject)
return;
Interactable interactable = m_CurrentObject.GetComponent<Interactable>();
CubeScript.onVRTriggerDown();
SphereScript.LoadScene();
}
}
There seems to be a small mistake in SceneManager.LoadScene("1");. If you want to load a scene by its built number and not by its name, you'll have to put in an integer and not a string. So unless your scene is named "1", this won't do the trick. Try SceneManager.LoadScene(1); instead.
Related
I am trying to create an infinite runner.
My scene have a "road creator" object that is just a trigger and a script attached to my road prefab.
Every time a piece of road leaves the road creator trigger, a new piece of road is created.
Here's the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class roadGenerator : MonoBehaviour
{
public GameObject road;
private void OnTriggerExit(Collider other) {
if(other.gameObject.tag == "roadGenerator"){
Instantiate(road, new Vector3(200, 0, 0), Quaternion.identity);
}
}
}
My issue is that once the player loses and the level reloads the script stops working.
Everything gets loaded properly (the trigger object and the first piece of road with the script attached) but for some reason the script doesn't get triggered...
Hey I think I'll need some more context to understand your problem:
You have a "roadGenerator" with a trigger and the Script attached + a "Road" Prefab with the Tag "roadGenerator" right?
I suppose by "realoading the level" you mean reloading the Scene with something like SceneManager.LoadScene(...)
It would also be good to know what you already tried out to fix it. :)
I am returning to unity after 6 months. I started my fps game yesterday. Today I was trying to make C4 bomb, which worked as follows: the c4 will already be placed, you just have to activate using a remote. In the start the remote will not be in your hand, you have to press F1 to set it active. To put the remote away again, we have to press F2. But the problem is that it won't detect my F1 key press. Here's the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class C4Script : MonoBehaviour
{
public GameObject remote;
private bool remoteHand;
// Start is called before the first frame update
void Start()
{
remote.SetActive(false);
remoteHand = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.F1) && !remoteHand)
{
remote.SetActive(true);
remoteHand = true;
}
if (Input.GetKeyUp(KeyCode.F2) && remoteHand)
{
remote.SetActive(false);
remoteHand = false;
}
}
}
The code may be wrongly formatted, I am sorry for that.
Also sorry if I didn't explain it properly.
Thanks!
The code works on my machine, do you maybe have the C4Script or the Gameobject on which it is attached disabled?
Looks like the script is never called rather then that it's not detecting the input. You could add an Debug log or debug breakpoint to the start method to check if it's ever called.
Soo, the problem was that i am dumb. i Put this script on the remote, and at the start it disables the remote thats why it wasn't detecting my key press. I put the script on my player object and now it works fine.
My code is really simple :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
public class move : Agent
{
public override void OnActionReceived(ActionBuffers actions)
{
Debug.Log(actions.DiscreteActions[0]);
}
}
I have "Discrete Actions" enabled in the Behavior Parameters and I'm using 1000 Max Steps. I'm using a simple Cube as a GameObject with those 2 scripts attached - the Behavior Parameters and the test script. I am running "mlagents--learn" and I get no errors or warnings it cannot connect (I do if I don't run mlagents first, so I'm asuming it has a connection). Is there any reason why I never get any actions? It's been giving me a headache for a few days, help would be greatly appreciated.
Thank you!
I am new to Unity so go easy on me. :)
I added an game object with a text field component (via TextMeshProUGUI) to my heads up display in my scene. I want to use this to display various statistics on it for debugging purposes during game play.
I then created a script which I added as a component to the same game object that holds my text component. Is this the best practice? Not sure how else I would get the script to execute.
Once I had my script created, I needed to find the text component as well as some other components in my scene so I could display the debug information. Below you can see how I did it... it feels a little dirty to be searching the entire scene to find these things. Would love some insight on how long-time Unity programmers go about this!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class PlayerDebugStatistics:MonoBehaviour {
TextMeshProUGUI playerDebugStatisticsText;
PlayerCharacterController playerCharacterController;
Health playerHealth;
private void Start() {
// First find the object in the scene named "PlayerDebugStatisticsText" and then get it's TextMeshProUGUI component
this.playerDebugStatisticsText = GameObject.Find("PlayerDebugStatisticsText").GetComponent<TextMeshProUGUI>();
// Get the player character controller
this.playerCharacterController = GameObject.FindObjectOfType<PlayerCharacterController>();
// Get the player health from the player character controller
this.playerHealth = playerCharacterController.GetComponent<Health>();
}
void Update() {
// Update the text every frame
this.playerDebugStatisticsText.SetText(string.Format("{0:N2}", this.playerHealth.currentHealth));
}
}
3 ways
Create an inspector reference to the other object and access it from your script - a public or private (with [SerializeField]) attribute in your script - and drag the component in like in this video: https://youtu.be/dMgQOP7kdxg?t=425
Use the singleton pattern: https://www.youtube.com/watch?v=5p2JlI7PV1w
Use dependency injection - https://github.com/modesttree/Zenject
Your way isn't terrible if you don't do it in the Update() loop, and cache your objects (like it appears you are doing), but if you need that performance in Start(), the above options can help.
I am getting errors using PlayerPrefs in Unity 4.6. I keep getting the error message saying "PlayerPrefs does not exist in the current context." I get the same message in MonoDevelop when I move the mouse pointer to hover over the PlayerPrefs statement. Any suggestions are appreciated.
maybe you are not in a true context means not in a class extended from monoBehavious
your desired code should look like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KiaStart : MonoBehaviour
{
// you can use PlayerPrefs here!...
void Start()
{
}
void Update()
{
}
}