Unity Game Stops On Load Same Scene Again - unity3d

I have 7 scenes in my game. one of the scenes is "Playing" scene witch is my game play scene. in this game, one match includes 3 round. i save some of match data in "MatchManager" script that attached to MatchManager game object that have a singleton and don't destroy on load. other info are in a script(PSceneManager) that attached to a gameObject(PSceneManager) belong to "Playing" scene.
In the end of each round i change some data and then I Have To Load "Playing" scene again.
HERE IS MY PROBLEM: when it loads again it stops at very beginning and even don't enter "start" function (method).
(I have a singleton in PSceneManager scrip. I delete that and even check and looking for any other static value.(there were no more static variables))
I HAVE NO IDEA WHAT IS MY PROBLEM??!(any idea can be helpful)
HEEELP PLEASE...I'M STUCKS HERE...

I don't know exactly what the problem is. Some more information would be helpful.
If I would have had this problem I would look for the first and last moment the script still works.
Unity has already build this in for us. Just like Start() and Update() we have OnEnable() and OnDisable(). So if you add a Debug.Log() statement in those MonoBehaviour functions you know if the script and object are still active.
void OnEnable() { Debug.Log( gameobject.name + " is enabled" ); }
void OnDisable() { Debug.Log( gameobject.name + " is disabled" ); }
https://docs.unity3d.com/Manual/ExecutionOrder.html

I have faced the same problem, and I found that before the loading of the other scene again I changed the Time.timeScale to zero.
I solved the problem by reassigning its value in the start of any game object of the scene that stops.

Related

Loading a scene in Unity twice causes some game objects to lose some components

When I hit play the game scene behaves as normal, even if I come from a different scene.
But when I am on the game scene and go to other scene and back again to the game scene some components from some game objects.
I am using SceneManager.LoadScene("SceneName"); to move around scenes, and every component was added in the editor and saved in the scene.
And as an example here is the Game Manager inspector at first load of game scene:
And second load of game scene:
Windows Controls is a script much like the others missing.
Am I doing something wrong?
Thanks in advance.
Without knowing what's going on inside the GameManager and the FoodManager scripts, I think you use the DontDestroyOnLoad similar what is showed on the bottom of the page, and your code destroys the scripts.
Simply put, I was short-sighted, I used a singleton pattern to access the managers and the way I did, at least, doesn't work in this context.
Any manager that I needed access I created a static instance, but since I checked for its existence elsewhere, the second time the component was destroyed.
public GameManager Instance {get; private set;}
void Awake()
{
if(Instance is not Null && Instance != this)
{
Destroy(Instance);
return;
}
Instance = this;
}
I realize that every time the scene loads the components are new objects, but I am still to understand why there is still copies of the old components.
I fixed it by using the [SerializeField] on any manager reference that I could and and manually adding them on the inspector, for some game objects that were instantiated at run time I pass the manager in a method.
GameManager gameManager;
public void Manager(GameManager _gameManager)
{
gameManager = _gameMananger;
}
GameObject gameObject = Instantiate(prefab);
gameObject.GetComponent<Script>().Manager(this);

Unity start Audio at specific moment

I am new to Unity and I have a little menu which comes up with the logo coming into the screen. Now I want to add a mp3/wav which gives an audio while the logo comes into the screen. The audio should stop when the logo appearance also stops.
How can I put the audio at the exact same moment when the logo (png) appears?
Thank you in advance :)
It might be an overkill solution for your simple case but you could try using Unity Timeline
https://docs.unity3d.com/Packages/com.unity.timeline#1.5/manual/index.html
You can setup all the menu animations with sounds and then play it on click.
https://learn.unity.com/tutorial/starting-timeline-through-a-c-script-2019-3#
All you need to do is have a reference to the AudioSource that has the audio you want to play and use the Play and Stop methods. It is also important to assure you have the field PlayOnAwake turned off as that will play the audio the moment the scene starts.
I do not know how you are currently moving your object, so here is a very generic answer as to how you can approach this using the information I gave above.
[SerializeField] private AudioSource yourAudio = null;
private void IEnumerator Movement
{
yourAudio.Start();
while(movement)
{
// your movement code here (assure there is a yield return null)
}
yourAudio.Stop();
}
With some code or more detail, I can tweak the above snippet but the main idea can be applied to however you are doing your movement. If you want to stop the audio the moment the logo appears on screen, that is a different approach, so let me know if that is what you meant.
Just for completeness, if you want to just stop or start this audio whenever your logo is visible or no longer visible, here is an approach. Be warned though, this approach will be triggered by the editor camera as well, so if something unexpected happens close or toggle off your scene view.
public class YourLogoScript : MonoBehaviour
{
[SerializeField] private AudioSource yourAudio = null;
// stop audio when invisible
void OnBecameInvisible()
{
yourAudio.Stop();
}
// start audio when visible
void OnBecameVisible()
{
yourAudio.Start();
}
}
By using OnBecameInvisible and OnBecameVisible, whenever the renderer attached to your logo is seen by any (including scene view) camera, the OnBecameVisible function is called and when it is no longer seen, the OnBecameInvisible is called.

Unity, keep playing music on different scenes

So I've been looking around and no solution seems to work to me...
I want to play some background music in all the scenes without resetting the audio.
I've a prefab with an audio source and a scritp.
the script does:
private void Awake()
{
GameObject[] objs = GameObject.FindGameObjectsWithTag("BGAudio");
if (objs.Length > 1)
Destroy(this.gameObject);
DontDestroyOnLoad(this.gameObject);
}
So I also tagged the prefab with BGAudio but when i change the scene it stops the music. If i add the prefab to both scenes it starts from 0..
I tried also doing a singleton but that doesn't work either.
I'm using unity 2019
I'm using an android build which i doesn't think it changes anything but just in case.
Just found out that DontDestroyOnLoad only works for objects on the root. While my Bgsound was inside a bg empty object.
You can also create a brand new scene that only includes your BGAudio object. Load both your BGAudio scene and play scene as additive.
For example: create a BGSound scene. Then create an empty gameobject called BGSoundManager. Also create your BGSound object. Then add a BGSoundManager script to your BGSoundManager gameobject and edit it like that :
private IEnumerator Start()
{
yield return SceneManager.LoadSceneAsync("BGSound", LoadSceneMode.Additive);
yield return SceneManager.LoadSceneAsync("Level1", LoadSceneMode.Additive);
Destroy(gameObject);
}

OnMouseOver works differently on same objects

I am a beginner in Unity and I just found out a behaviour I do not understand...
I have a prefab "cell" that I made from a sprite and I want it to change its color when my mouse is over it.
So I added a BoxCollider2D component to it as well as the following script:
public class Cell : MonoBehaviour
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
private void OnMouseOver()
{
GetComponent<SpriteRenderer>().color = Color.magenta;
}
private void OnMouseExit()
{
GetComponent<SpriteRenderer>().color = Color.black;
}
}
Then when I drag and drop the "cell" prefab to the scene, it won't work (when my mouse is over the cell, nothing happens).
Same problem when I add another "cell" prefab to the scene.
But when I add a third "cell" prefab to the scene, the feature works on the 2 first cells but not on the third.
I probably missed something or there is a behaviour I do not know, anyway if someone knows why this is happening, please tell me.
Thanks!
I just tested your code in my game using a 2d box sprite and it works fine.
Video > https://youtu.be/6GP3-aV9g3g
You may want to try a couple of things to debug it.
First make sure that there is a BoxCollider2D and Rigidbody2D attached.
Make sure that there is nothing covering the boxes in the scene.
When I am having trouble with an aspect of the game I try to break it down into its simplest components. Try making a scene with nothing in it apart from the box and trying it, if that doesent work, try attaching the scrip to a non prefab object.
Try adding Debug.Log("Mouse Enter"); to the subs to check if the mouse is detected on enter, if it is detecting the mouse, maybe your spite renderer isn't working properly.
Try these things and let me know if they don't work, I would be happy to keep trying to figure it out.

Ending my 2D Game in Unity

I have been busy making a 2D game for the past month and am really happy with the way it has turned out... However my destroyer (collider) to end the level and send me to my other level which has the info on score etc isn't working how I would like it to..
Here the script on the Destroyer:
using UnityEngine;
using System.Collections;
public class EndGameDestroyer : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player") {
Application.LoadLevel(2);
return;
}
}
}
Im using C# btw
Currently I run through the level and see the Destroyer in the background but then it just disapears and doesnt end the level. Please help as I am showing my game to the public at a games expo my college is running tommorow...
Thanks in advance :D
Have you added the scene for level 2 to your build? If not you need to go to your build settings while in your level 2 scene and press "add current" below the scenes in build box. Then go back to whatever scene you were in previously to get to the trigger and see if that works.
first maybe you need to check is your player already in "player" tag
second i suppose your scene name 2
open your scene 2 and then file>Builds Settings and see if your scene 2 is listed in the scene list and checked if not then just click add current
and then
Application.LoadLevel("2");
it need string