I have a scene that I'm working on using Steam VR 2.0, and Unity 2018.3.2f1. I have a simple statement in it that reloads the scene
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadSceneAsync("Final");
}
}
The issue is: when I reload the scene, it stops responding properly. I am still able to move my head around, and hover over objects. And the objects I hover over get highlighted, but they stay highlighted. I'm not able to pick them up, or interact with them in any meaningful way, and I don't know why this is happening.
I've attached a screenshot of the issue below.
As you can see, multiple objects are highlighted, and the hand mesh is weird:
Solutions I've tried--
Using LoadScene instead of LoadSceneAsync
Using Application.LoadScene instead
Tried to edit the Player script in SteamVR library to not add it to Don't Destroy On Load
Any suggestions?
The issue was arising because the Player prefab in SteamVR 2.0 had Do Not Destroy on Load checked. So, there were multiple players being instanced when I reloaded the scene. I unchecked that box, and now everything is in order.
The checkbox is located inside the [SteamVR] object under the Player prefab:
Related
So I've just been tinkering with unity and all yesterday I was making a little game. I have a main menu when you start the game, the game itself and a gameover menu when you die that puts you back at the home screen, but I have no clue how to set it when a thing/enemy hits the player, It brings you to the restart menu on collision with it. anyone know any code to help out, I've been trying to find videos but they are not what I need. I need when on collision set scene to GAMEOVER. But don't know how, PLZ help.
So to what I understand you want to make a collision and restart you scene or go to another scene ?
You need to use something like this.
I added 3 possibiities of Scenemanager.LoadScene however you need to use them.
make sure to add the the using UnityEngine.SceneManagement; at the top
using UnityEngine.SceneManagement;
void OnCollisionEnter(Collision collision)
{
//if you want to load another scene
SceneManager.LoadScene("OtherSceneName");
//if you want to load the same scene you are on
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
//if you want to load the same scene you are on
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
To know if an object with a Collider was hit in Unity (e.g. the player) you need to add one of the OnCollision functions to your script:
OnCollisionEnter - is called when the object is first hit
OnCollisionStay - is called every frame while the object is being touched
OnCollisionExit - is called on the frame the object stops being touched
Then, in the function you can load a different scene using SceneManager.LoadScene
I have a audio player during scene 0 in my game (the menu). I have a code that intends to prevent the objects destruction when changing scenes. This works.. mostly. But for some reason, when moving to scene 1 a second audio player is created. This does not happen when moving to scene 2, 3, etc. I cannot find any components in scene 1 indicating a second audio player.
I have attached screen shots that may be of assistance. Any help with this would be greatly appreciated. I am stumped.
audio player and code ....
duplicated audio in scene 1....
In your Scene Changing Script, Create a New Function (or edit existing function)
LoadSceneWithoutAudioDestruction or Whatever seems good to you
Then Write down the Following Code.
public GameObject AudioSouce; //in starting of file to reference Audio
public void LoadSceneWithoutAudioDestruction(){
DontDestroyOnLoad(AudioSouce);
SceneManager.LoadScene(SceneNameasString);
}
Now on button which you want to Load next Scene attach this Script and ad function LoadSceneWithoutAudioDestruction to On Button click Event of that button.
Also Ensure that the next scene that you are Loading does not habe any Audio Source Already
If any Problem exists, ask me in comment.
I ought to have a normal map of tiles as below:
But when I load the scene, either switch from another in inspector (e.g. click "scene/menu", then click this scene), or transit into this scene in gameplay (SceneManager.loadScene), sprites get invisible randomly.
Each time some of the sprites get invisible, and the next time the invisible ones may not be the same. I suspect it is something with Unity editor, since this happens even when I'm not in playing mode.
In Debug.Log I definitely find those sprites (not null). They just don't show up. I have to reload sprites / reload the scene in Unity inspector, but the load doesn't persist.
Neither sprites, SpriteRenderer nor GameObject is null.
As below is a snapshot when some sprites invisible (not missing):
Just click another scene and switch back:
I just accidentally checked in 3D mode, Unity editor is randomizing with z axis. The sprites are there, they were just hidden behind background. This can be fixed then.
I have two scenes: Menu and Game. I'm using C#.
When you die in the game, you get sent back to the menu with
SceneManager.LoadScene("Menu");
There's a script with a GUIButton in the menu that, when clicked, loads the game with
SceneManager.LoadScene("Game");
What I want to happen is that when I click the button in the menu scene, it loads the game as if I just clicked the play button with the game scene open in the editor.
What instead happens is that it goes to the game scene, but some objects from the game scene appear to be missing. I'm not using DontDestroyOnLoad() anywhere.
Some objects from the scene do appear, but others don't. The weirdest thing is that there are some data fields on a script on the missing object that are referred to by some other scripts, and those give values that make sense.
Does anyone have any idea what's going on, or what I can do to get the desired result?
Turns out that the problem was that I was destroying one of my objects if it was not the first time that object was created, and I didn't realize it.
are those static objects or dynamic? try to instantiate them in function which gets called on scene loaded.
Tried to find this kind of problem around the net but failed so...
Here's the thing - I have a prefab gameobject that is to represent a unit, portrait more specifically. It has several scripts attached and an Animation component with two animations: Static and Selected.
The prefab is instantiated, moved freely and, after placing, it can be clicked to select it, which should, aside from executing a bit of code, start the Selected animation.
Using this code:
void OnMouseDown(){
//
//Some inside stuff
//
if (this.GetComponent<UnitHandling> ().thisUnit.Selected)
this.animation.Play("Selected");
if(this.animation.IsPlaying ("Selected"))
Debug.Log("Animation of selection is playing");
}
I checked that the animation seems to be playing (at least the Debug message is showing) but I see no animation... What am I doing wrong?
Try making an animation state using mechanim, and play it using this:
GetComponent<Animator>().CrossFade("Selected", 0);
https://docs.unity3d.com/Documentation/ScriptReference/Animator.CrossFade.html
https://docs.unity3d.com/Documentation/Manual/MecanimAnimationSystem.html