Hello!
- I got a camera with a background picture and a script attached to it
- The script work, but not always? O.o
The problem is that when I go from one scene, for example from the final level to the credits, the camera wont move as it’s supposed to do in the script. When I start the credits scene directly, it works…
What can the problem be, I need help ASAP. Also including some screenshots so you can understand the problem better. (The square area with the blue dots in the conors are the camera area).
How it looks before start:
How it looks from direct start and is supposed to be after a few secound when going to this scene from another:
How it looks everytime I go from one scene to the credits scene by clicking Next Level at goal:
Do you have a persistent camera object or anything else that has been used in DontDestroyOnLoad()?
I haven’t used DontDestroyOnLoad() at all :s
Hey,
I was thinking about your problem and I do guess I know what you did: You used Time.time * text.yCoordinate or something like that. You animated the text using the time. So when you start the scene directly, Time.time goes from 0 to xxx (text scrolls up) - but if you already played the game and you switch to the scene Time.time is already >0 (one could say its over 9000 - depending on how long you played). If you use this value as y Coordinate for text, it surely is out of screen.
Post your code for your text animation for further help. You would have to use something like
float pixelsPerSecond = 1; // move 1 px up every second
Vector3 pos = creditText.position;
pos.y += pixelsPerSecond * Time.deltaTime;
credit.position = pos;
Almost, I made my script so it moves the camera along the Y-axis. When the camera reaches the specific coordinates, (the end of the credits), it will freeze and a button will appear.
My friend, (who’s also a C# programmer), said that it maybe, just maybe had something to do with DontDestroyOnLoad(). These are tho pure speculations when none of us are professional programmers.
This is my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class EndGameCredits : MonoBehaviour
{
[SerializeField]
public float speed;
public float freezeCoordinates;
public bool creditsDone;
void Awake()
{
creditsDone = false;
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
//If the speed is equal to 0, start the timer and turn creditsDone to true
if (speed == 0)
{
creditsDone = true;
}
//When the camera is at these coordinates, it will stop
if (transform.position.y <= freezeCoordinates)
{
speed = 0f;
}
}
void OnGUI()
{
//If creditsDone is true, display the button that will take you to the Main Menu
if (creditsDone == true)
{
//If you press this, you will go to the Main Menu
if (GUI.Button(new Rect(Screen.width / 2.5f, Screen.height / 1.2f, Screen.width / 5, Screen.height / 10), "Main Menu"))
{
SceneManager.LoadScene("Menu");
}
}
}
}
Hm not what i thought. Can’t see error here
You could try animating the camera(or the image, either way) motion using mecanim. Depending on how you animate the motion you can have it as the default state or initiate it using a trigger. Near the end of the animation you can set an event marker (http://docs.unity3d.com/Manual/animeditor-AnimationEvents.html) that calls a function flipping your ‘creditsDone’ bool so that the button for returning to the main menu will appear.
If you do that you can get rid of the update and awake(you can get rid of that awake function anyway, just assign the bool to false on the line where you first define it.) functions and create a new one called ToggleBool (or whatever):
public void ToggleBool()
{
creditsDone = true;
}
And call that function using the event marker in your animation. This should only take a few mins to set up, sorry it doesn’t really fix the issue you are having, but its always good to consider alternate possibilities.
Sounds like a plan! I’ll try that and tell you if I got it to work here
If you could, would you kindly try to solve this error so I know what’s wrong with this current method and how I can avoid these in the future
SOLVED
My friend managed to find the problem!
In my “Victory” script, the game is paused and you get the option to go to next level. The pause function is Time.timeScale = 0f. In all of the other levels I got a Pause menu that has this code:
if (isPaused)
{
//If the game is paused, the music shall stop, the Pause menu will show and the game will freeze so nothing can happen
pauseMenuCanvas.SetActive(true);
Music.SetActive(false);
ShowMenu = true;
Time.timeScale = 0f;
}
else
{
//If the game is not paused, the music will play, the Pause menu will not show and the game will continue
pauseMenuCanvas.SetActive(false);
Music.SetActive(true);
ShowMenu = false;
Time.timeScale = 1f;
}
Because of the fact that my credits scene dont have the pause menu script, it wont check if it’s paused or not, and thus dont turn back to Time.timeScale = 1f;
The problem was easily solved by adding this:
void Start()
{
Time.timeScale = 1f;
}