I am a beginner with Unity3D.
Until now I have learned a few things.
For example, create a simple 2D menu, switch from one scene to another with an appropriate C# script and associate the scripts with the objects.
The problem I pose here is about the behavior (strange to me) of the white panel behind the menu when I change the scene.
I have two scenes, the first which is the menu and a second one is the actual game.
I used UI elements to create the first one, while I used 3D objects to make the second one.
So, what happens?
It happens that when you step into the second scene by clicking on the green button the background becomes brown. If, on the other hand, I run the second scene individually without going through the first one (launching the scene directly), the background remains white.
See this short video for further details.
Why?
Listed below are some images, hoping they can help me understand my problem.
The first scene (dev mode):
The first scene (running):
The second scene (dev mode):
The second scene (running directly):
The second scene (running after clicking the green button "Comincia"):
The script I use to change the scene (linked to the green button):
using UnityEngine.SceneManagement;
using UnityEngine;
public class GameScene : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void PlayNow () {
SceneManager.LoadScene("Game", LoadSceneMode.Single);
}
}
Update: I have updated the information on the application. I added a new screenshot where you can see the camera settings. Meanwhile, I changed some things, but the problem still remains. I also noticed that this problem only happens when I switch to the Game scene, when I switch to other scenes instead it does not.
Added a new screenshot showing the inspector of the Main Camera object:
I was able to understand where the problem was: I made an improper use of the Plane object (highlighted in the screenshot). By removing it, now it shows correctly the solid color white background as set on the Main Camera.
Related
So, to start, I have this code:
public void switcher(string scene_name)
{
SceneManager.LoadScene(scene_name, LoadSceneMode.Single);
}
That's supposed to load a scene given in the editor, and I have 2 buttons, each supposed to load a specific scene and 2 gameobjects (one for scene) that have the code written before.
Object with script
Button that calls object
Now, the thing is, despite having different scene names, both buttons load the same scene, and when I do some changes (mainly deleting and re-adding components) then both buttons load another scene, but never one scene each.
Why is this happening?
First of all to load your scene you must add it to the build settings.Go to File>Build settings and then drag your scene into the Scenes in build.Sencond thing you must add this line of code to set you scene to be the active one
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + *Add you scene number here*);
I have a script called StartGame that is suposed to do different things when different game modes are selected. Unity's API and forums suggests using OnLevelWasLoaded, but it isnt being called. In fact when I hover my mouse over it in Visual Studio it tells me the method is"StartGame.OnLevelWasLoaded". I'm pretty sure that means that It isn't overriding the method. Has anyone else had this problem? Also I'm using Unity 5.3.
Are you sure your GameObject is still existing after loading the new scene?
Usually, all GameObjects are deleted from the scene when loading another scene.
You can prevent this by loading the scene additive (adding the contents of the new scene to the contents of the current one)
SceneManager.LoadScene("your scene name", LoadSceneMode.Additive);
or by preventing deletion for your specific GameObject
GameObject.DontDestroyOnLoad(yourStartGameScriptHolder); // where yourStartGameScriptHolder is the GameObject(!), not the script reference
I made an animation using Animation panel, which swaps images from time to time. Read from the Internet, this is not a legacy animation.
Here is the Animation panel screenshot:
Then, I add the Animation and Animator components to the Game Object and assign the animation, which is called Animation01, to it. Here is the screenshot from Inspector of the Game Object:
I try to use the following C# script to stop the animation :
using UnityEngine;
using System.Collections;
public class Scene1 : MonoBehaviour {
public GameObject ball;
// Use this for initialization
void Start () {
ball.animation.Stop();
}
// Update is called once per frame
void Update () {
}
}
but the animation didn't stop. It prompts a notice in Console:
Default clip could not be found in attached animations list.
What did I miss?
UPDATE: By disabling/ removing Animator component, the animation is stopped and cannot be controlled by codes. I need to control the animation by codes.
Unity had implemented two animation systems throughout it's history. Animation component belongs to the legacy animation system, while Animator component belongs to the new animation system. So, adding both components makes no sense: you either use the old system or the new.
The main difference between the legacy and new animation system is that the new animation system is much more advanced in way it's controlled. However, it also means that it's more complicated. It adds another level of abstraction: instead of launching animation yourself, you control variables that influence the behaviour of a special state machine, animation controller.
So, if you want to use animations for something really, really simple, where you want just to launch animations manually, it may be better to use legacy animation system instead. But the components are not the only thing that is different: the animation files themselves are marked to determine if they are "legacy" or not. By default, when you create an animation in the new unity version, it belongs to the new animation system. If you want to use it with a legacy animation, you have to mark it as a legacy animation. Unfortunately, you'll have to do a little hack to achieve that.
is there anyway to draw stuff on scene view without calling OnSceneGUI?
or is there a way to get OnSceneGUI to get called even if the object with the script attached is not selected?
Edit: Looks like I wasn't explicit enough on what I was asking for so here's a little update:
I have a whole bunch of controls shown as GUI objects on my Game Scene that are used by the game designers to more easily test the game. Since these tools are for development use rather than deployment, I wanted to move these to the scene window instead.
Unfortunately I am only able to display these GUI tools if the object that contains the script with the "OnSceneGUI" method is selected. Instead I want them to be displayed whenever the game is running and regardless of the selected object.
I have found the solution to my issue.
If you want your GUI stuff to always be displayed on the scene window you can do the following:
public class MyClass{
static MyStaticConstructor() {
SceneView.onSceneGUIDelegate += OnScene;
}
static void OnScene(SceneView sceneView) {
// Draw GUI stuff here for Scene window display
}
}
This code will run as soon as you press play and will draw whatever you wish on your scene window.
Hope it will help others!
There are many ways to use GUI,
The easiest but least efficient way is using the GUI class on OnGUI()
GUI.Label(new Rect(10,10,10,10), "This is a label");
GUI.Label(new Rect(10,10,10,10), "Your Texture2D here");
Any active monobehaviour will run OnGUI() if its defined. So it can be attached to any gameObject. You can create an empty gameObject in the scene and call it "GuiGameObject" for example and attach the script there. That way it wont be mixed in with your gameplay script.
There are also GUI textures --> More Info on GUITexture
Also I recommend checking out nGUI
Edit:
For OnSceneGUI You can try Editor.Repaint, you can use it to make sure that the inspector updates changes made inside of OnSceneGUI
I'm trying to play two animations one after another in unity (each animation is two separate paths for the main camera) and from my research I think I need to use the following:
// Update is called once per frame
void Update () {
animation.PlayQueued("CameraMovement", QueueMode.PlayNow);
animation.PlayQueued("SpaceSceneMovement", QueueMode.CompleteOthers);
}
However, with this code attached to my camera, my game still only plays the first path. I created the paths so that there should be half a second delay between the different paths. I have both animations in the animation component of my camera (see attached pics) and I'm stumped as to why this isn't working.
Am I missing something?
As stated by user2025312, you are calling the code each frame. Which means that each frame you're telling it to play the first animation immediately, and you queue the second one. That won't work.
Depending on what you want to do you have several options. Say you want to continuously play the two animations in a loop. What you could do is:
void Update () {
if (!animation.isPlaying)
{
animation.PlayQueued("CameraMovement", QueueMode.PlayNow);
animation.PlayQueued("SpaceSceneMovement", QueueMode.CompleteOthers);
}
}
As should be easy to follow, that checks whether or not there is an animation playing, and if not, it will queue the two, playing the first one immediately. That will work fine within Update().
If however you're looking to play the animation sequence just once (or upon a trigger) place the two lines within Start() or a method of your choice.
void Start () {
animation.PlayQueued("CameraMovement", QueueMode.PlayNow);
animation.PlayQueued("SpaceSceneMovement", QueueMode.CompleteOthers);
}
That should work just fine. If not, verify that your animations work individually.