Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LordObsession · Sep 13, 2016 at 11:01 AM · c#sceneloadcoins

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!

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Sep 13, 2016 at 12:05 PM 0
Share

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?

avatar image LordObsession · Sep 13, 2016 at 12:16 PM 0
Share

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

avatar image tanoshimi LordObsession · Sep 13, 2016 at 12:21 PM 1
Share

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);
     }
  }
avatar image LordObsession tanoshimi · Sep 13, 2016 at 12:31 PM 0
Share

Awesome! It works, thanks Tanoshimi

avatar image Hydropulse17 · Sep 13, 2016 at 12:17 PM 0
Share

I assume you have set up the scenes in the build settings?

avatar image LordObsession Hydropulse17 · Sep 13, 2016 at 12:21 PM 0
Share

Yep, I have all scenes set up in build settings.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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++;

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LordObsession · Sep 13, 2016 at 12:00 PM 0
Share

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.

avatar image Tabu LordObsession · Sep 13, 2016 at 12:26 PM 0
Share

Have you tried to load the scene using the name of the scene ins$$anonymous$$d of its number from the build settings?

avatar image LordObsession Tabu · Sep 13, 2016 at 12:36 PM 0
Share

Haven't tried that but Looks like its working now? Thanks for all the help. :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

IS it possible to load the whole game (and all scenes) at startup to avoid scene change delay? 1 Answer

How to add a transition between scenes? 1 Answer

How to Load PlayerPrefs from a different scene 1 Answer

Play Audio Through Scenes 0 Answers

Multiple Cars not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges