- Home /
Load another Scene after collecting 6 coins
Hi, cant figure out why this C# script is not working. I just want to go to a new scene after collecting 6 coins/selfies This the code I have on my player.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class CoinController : MonoBehaviour {
public static int coinCount = 0;
void OnGUI()
{
string coinText = "Selfies: " + coinCount;
GUI.Box (new Rect(Screen.width - 150, 20, 130, 20), coinText);
}
void Update ()
{
if (coinCount >= 6)
SceneManager.LoadScene (1);
}
}
And this one is on the coin/selfie
using UnityEngine;
using System.Collections;
public class PickupItem : MonoBehaviour
{
AudioSource _audioSource;
public AudioClip _audioClip;
public GameObject _particle;
void Start()
{
_audioSource = GetComponent<AudioSource>();
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
Renderer[] renderers = GetComponentsInChildren<Renderer>();
foreach (Renderer r in renderers)
r.enabled = false;
_audioSource.PlayOneShot(_audioClip);
CoinController.coinCount++;
Destroy(gameObject, _audioClip.length);
}
}
}
}
Thanks in advance!
You've said what you expect to happen, but not what actually happens at the moment.
Do you see the "Selfies: 6" display at the top of the screen?
Does audio play each time you pick up a coin?
Does anything display in the Console Log? What about if you add
Debug.Log("Loading Next Scene");
just before your Scene$$anonymous$$anager.LoadScene call?
Yes it does say Selfies: 6 at top of screen. the Audio does play and when I add Debug.Log("Loading Next Scene");
in front of Scene$$anonymous$$anager.LoadScene
I get CS8025: Parsing error
You shouldn't get a parsing error. Check you have as follows:
void Update ()
{
if (coinCount >= 6) {
Debug.Log("Loading Next Scene");
Scene$$anonymous$$anager.LoadScene (1);
}
}
I assume you have set up the scenes in the build settings?
Yep, I have all scenes set up in build settings.
Answer by Tabu · Sep 13, 2016 at 11:24 AM
In the PickupItem object, did you remember to set the collider to "trigger" ? Not sure why you are not getting the CoinController object? GetComponent<CoinController>().coinCount++;
Yes, the coin's box collider is set to trigger. It script does count the coins collected, but it does not load the new scene after 6 have been collected.
Have you tried to load the scene using the name of the scene ins$$anonymous$$d of its number from the build settings?
Haven't tried that but Looks like its working now? Thanks for all the help. :)