Unity C# - Hide / Unhide a Button in a different scene - c#

I'm exploring a bit in Unity and C#, I searched everywhere for a solution without luck.
Problem:
In Scene A, I have an object button called "Button_2" -> it's hidden.
In Scene B, I have another button called "Button_1", and this one, must unhide "Button_2", in the other scene "Scene A"
How can I achieve this, if possible of course?
Thank you.
EDIT 1:
I tried the following:
Used DontDestroyOnLoad. Basically can't use it on a single button. Unity doesnt allow it. So, I had to do it on the canvas, but when I switched to another scene, was a huge mess with both scenes mixed.
Tried Yuris solution and suggestions and didn't work. Scenes just don't interact with each other at all.
Tried to combine both of above with the prefab concept Yuris suggested aswell and still no luck.
I will keep doing my best, but my knowledge is very limited. I'll be back with more feedback if I make progress.
EDIT 2:
Made many experiments with a non monobehaviour script file and class with only variables to be accessed by a script file that is linked to gameobjects in the diferent scenes.
I wasnt hable to access the variables on the non monobehaviour file. Looks like unity doesn't like non monobehaviour and didn't work.

You can make the button a prefab and assign it on a script in the Scene B, and then when you load scene A again, it will be active.
Remember that if you use that prefab anywhere else, it will be active there too.
Example:
Example.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Example : MonoBehaviour
{
public GameObject button;
// Start is called before the first frame update
void Awake()
{
button.SetActive(true);
}
}

Related

How to disable GameObject after animation is finished

So I have an animation that fades into the menu screen but after the animation ends none of my buttons work. I have figured out that it is because the GameObject that holds the black image that fades to clear is always in the front, blocking me from using any of the buttons. I tried to write a script, that's attached to the game object, that disables the GameObject after it completes the animation, but it isn't working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelChanger : MonoBehaviour
{
public Animation anim;
public void SetTrigger()
{
this.StartCoroutine(this.PerformAnimRoutine());
}
private IEnumerator PerformAnimRoutine()
{
var state = anim.PlayQueued("Fade_In", QueueMode.PlayNow, PlayMode.StopSameLayer);
yield return new WaitForSeconds(state.length);
this.gameObject.SetActive(false);
}
}
Is there anything wrong with the code or is there an easier way to accomplish this? I am extremely new to unity so I am very stuck.
If all you are doing is fading a sprite to clear and you seem to know about coroutines, I might start by suggesting you do the fade within a coroutine instead.
Have that decrease the alpha by some fraction each frame and when it's 0 disable the object.
That's just if that sounds more fluid, nothing wrong with the animation way.
Doing it with animations though :
I'm not confident that you can disable the object the animation is on in that animation. If it is available on the dope sheet try that. Otherwise we can use state behaviours or animation events.
Animation Events
These can be used to trigger a function at a certain point of an animation. You can create them similar to keyframes. Here is a link to Unity's guide on this topic.
All you'd need to do is create an event and place it at the end of the animation. Then you need to in a script of that object make a public function that simply disables the object. Call that with the event.
State Behaviours
State Machine Behaviours allow you to define a script to run on a given animation state. It has many functions to hook onto such as OnStateEnter and OnStateExit.
You'd want to click on the state that fades in the animator. In the inspector you should be able to click "Add Behaviour". This will create a script that you can open and edit. Here is the reference for that class.
From there is should be very simple to disable the object through OnStateExit.

Need clarification on singletons + game objects

I'm still a bit lost on this. Basically I want to have a persistent gameobject throughout multiple scenes. This gameobject will represent the players avatar. It is displayed using a prefab.
I tried adding the singleton script to my login scene controller. I have a public GameObject class parameter but it isn't showing up in the inspector so I can drop the prefab in. Maybe I'm getting this wrong.
Also, let's say I'm testing a scene. Since the singleton is initialized in the login scene, How would I get this global gameobject prefab into that new scene I'm testing considering I won't even be loading the login scene during testing?
I think I'm just super confused. All I want is to be able to use 1 script which represents the players "avatar", including the prefab used to display it, and have it persist throughout the entire game. Is this possible? Also, how do I get it when I'm testing scenes and haven't called the scene containing the singleton?
Thanks for anyone who can help me.
Basically I want to have a persistent gameobject throughout multiple scenes. This gameobject will represent the players avatar.
Use DontDestroyOnLoad on the object that should persist throughout multiple scenes.
Take caution if you are loading back the same scene that set the Object to DontDestroyOnLoad, as it will create another same object that has a DontDestroyOnLoad. This post talks about it.
Edit (Thanks to yes), check the comments in this answer.
(There are also a few more options that you have depending on your needs, Hugo's answer from this post has it.)
I tried adding the singleton script to my login scene controller. I have a public GameObject class parameter but it isn't showing up in the inspector so I can drop the prefab in. Maybe I'm getting this wrong.
Show some codes? It is hard to exactly tell what is wrong.
It might be the fact that you forgot to drag in your script to a game-object, or the class is static, etc.

Strange plane behavior in Unity3D while changing scene

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.

Create GameObject and save in Hierarchy

I would like to create gameObjects (text, 3d stuff, etc.) from scripts and save them in the scene.
For example: If I use a script like this,
public class ExampleClass : MonoBehaviour
{
public void Start()
{
GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
}
}
the "plane" will be add in Hierarchy temporarily (just after you press start).
I'm looking for some way to create a lot of gameObjects automatically (through a script, api, etc.) and save them in the hierarchy.
you are talking of persistent GameObject after stopping scene i suppose.
The only possibility to make gameObject instance pop inside your scene hierarchy and stay after scene played and stopped is to use CustomEditors.
You have to know, unity editor is based on Unity Gui, so you can easily add buttons make it run some specific script coded by you, inside windows menu (for example).
But you can also custom the mouse right click on scene hierarchy to allow peoples to create instances of a allowed gameObject and add this created instance directly on child of previously clicked gameobject from the scene etc...
Or make create your own tools script, and add it as component on Gameobject and with specific layout for it and rendered by Unity Editor.
For use customEditor step by step :
1 - Create a folder named Editor inside Assets/ root project hierarchy
2 - Create a script like bellow, which is using reference to UnityEditor and add you could use the commented Attribute. At the end you have to make your class extend From Editor class.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
// uncomment this line bellow if you want to create customEditor script which you will be able to use on gameObject as component.
// typeof give script type , it's a script created as component and extending from Monobehaviour, he contain value etc.. he could be manipulated from this CustomEditor script via button who call Methodes.
//[CustomEditor(typeof(YourScriptComponent))]
public class MyNewCustomEditor : Editor
{}
for more examples of customEditors script look at unity doc: http://docs.unity3d.com/ScriptReference/Editor.html
3 - Create methodes to be used and called out of scene Playing mode.
you have to create buttons inside OnInspectorGUI() function which will call your methodes for instantiate inside scenes some GameObjects.
4 - WARNING , To instantiate resources like prefabs etc, you have to put yours needed resources inside folder called Resources and use inside your script the Resources.Load() methode.
see the doc for more information , it's pretty simple to use : http://docs.unity3d.com/ScriptReference/Resources.Load.html
Hope it will help, see You.
There was a similar question asked over here.
You want be making an Editor Script then using pretty much the same logic you would use at runtime. Here is the Unity Docs on editor scripts.
You can just make a C# script and put it in the "Editor" folder. If you want it to appear in the editor as a dropdown option you can throw in "[MenuItem("MyTools/CreateGameObjects")]" before a function as shown in the other Stack Overflow answer.

Drawing GUI stuff in scene view Unity3d

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

Categories