- Home /
Scene doesn't play when linked from outside
Heya Unity Answers,
I'm completely stuck on what to do. I've created a little project consisting of 3 scenes - a normal game scene, a main menu and a "cracked" game scene. The "cracked" and normal version of the scenes share a bunch of assets - mainly the player asset.
When I play the "cracked" scene from the editor or from the main menu, it works fine. However, when I play the normal scene, proceed into the main menu, and then onto the "cracked" scene everything just stays still. No animations, no sounds, nothing is happening. Unity doesn't crash, the Build Settings are also fine, so it must be something that happens between the normal scene and the "cracked" scene - some code freezing it up maybe? I read online that loops could cause this but as far as I'm aware I don't have any...
This is my player code, it's quite messy but it's a work in progress!
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class PlayerMovement : MonoBehaviour {
private int health;
private bool canJump;
private Rigidbody rigidbody;
public int jumpstr;
private CameraScript cameraScript;
public float rotateSpeed = 70f;
public int score;
private float scoreTime = 0.0f;
public GameObject inputButton;
private AudioSource audioSource;
public AudioClip skatejump;
public AudioClip barrelbreak;
public AudioClip skateland;
public AudioClip munch;
public Text scoreText;
public ParticleEmitter bloodSplat;
public float hunger;
private float hungerTime = 0.0f;
private NPCMovement npcMovement;
private Scene scene;
// Use this for initialization
void Awake () {
health = 6;
score = 0;
rigidbody = GetComponent<Rigidbody> ();
cameraScript = GameObject.Find ("Manager").GetComponent<CameraScript> ();
inputButton.SetActive(false);
audioSource = this.gameObject.GetComponent<AudioSource> ();
ScoreCounter ();
transform.FindChild ("hitText").gameObject.SetActive (false);
hunger = 100;
scene = SceneManager.GetActiveScene ();
}
public void Damage()
{
health -= 1;
score -= 1000;
this.StartCoroutine (this.HitAnim ());
if (health < 1)
{
GameOver ();
}
}
public int GetHealth()
{
return health;
}
// Update is called once per frame
private void Update ()
{
scoreTime += Time.deltaTime;
if (scoreTime >= 1) {
score += (int)scoreTime * 5;
scoreTime -= (int)scoreTime / 2;
ScoreCounter ();
}
if (scene.name == "Cracked") {
transform.position += transform.right * Time.deltaTime * 30;
Debug.Log ("let'sgobois");
}
if(Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space))
{
if(canJump == true)
{
Debug.Log ("rekt");
rigidbody.AddForce (new Vector3(0, jumpstr, 0), ForceMode.Impulse);
canJump = false;
audioSource.Stop ();
audioSource.PlayOneShot(skatejump, 0.5f);
}
}
if (transform.position.y < -2) {
Debug.Log ("Game Over");
GameOver ();
}
HungryHungryHippo ();
if (hunger <= 0) {
GameOver ();
}
if (hunger > 100) {
hunger = 100;
}
}
private void FixedUpdate()
{
if (cameraScript.transition == true) {
if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) {
transform.Rotate (Vector3.up * rotateSpeed * -Time.deltaTime);
}
if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) {
transform.Rotate (Vector3.up * rotateSpeed * Time.deltaTime);
}
}
}
private void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "ground") {
canJump = true;
audioSource.Play ();
audioSource.PlayOneShot (skateland, 0.5f);
}
else if (col.gameObject.tag == "NPC") {
Yummy ();
npcMovement = GameObject.FindGameObjectWithTag("NPC").GetComponent<NPCMovement> ();
npcMovement.DeadPygmy ();
}
}
public void BreakBarrel()
{
audioSource.PlayOneShot (barrelbreak, 0.5f);
}
public void GameOver()
{
inputButton.SetActive (true);
Time.timeScale = 0;
audioSource.Stop();
scoreTime = 0;
}
public void ScoreCounter()
{
scoreText.text = "Score: " + score.ToString();
}
private IEnumerator HitAnim()
{
transform.FindChild ("hitText").gameObject.SetActive (true);
transform.FindChild ("hitText").gameObject.GetComponent<Animation> ().Play ("hitAnim");
yield return new WaitForSeconds (0.2f);
transform.FindChild ("hitText").gameObject.SetActive (false);
}
public void Yummy()
{
Debug.Log ("hunger went down!");
hunger += 10;
audioSource.PlayOneShot (munch, 0.5f);
bloodSplat.enabled = true;
}
public void HungryHungryHippo()
{
Scene scene = SceneManager.GetActiveScene ();
if (scene.name == "Cracked") {
hungerTime += Time.deltaTime;
if (hungerTime >= 1) {
hunger -= (int)hungerTime * 1;
hungerTime -= (int)hungerTime / 1;
Debug.Log ("hunger: " + hunger);
}
}
}
}
Any help would be insanely appreciated!
Well as it turns out I totally forgot that I added time.timescale = 0 in my code (been coding for many hours so tired). setting it to 1 on start fixed my issue!
Answer by TheTurkeydipking · Dec 11, 2016 at 11:07 PM
My silly mistake. Timescale was set to 0, had to reset it to 1 to fix it!