Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed May 09, 2013 at 01:23 PM by Fattie for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Nelis · May 09, 2013 at 09:53 AM · javascriptscenefunctionaccess

How to start a function from another scene.

Hello, I've searched for the answer of this question but I could not find it. The question is really simple, I have in one scene this script:

 function OnGUI(){
         if(GUI.Button(Rect(267,0,77,33),"Next Wave")){
             Application.LoadLevel("Asteroid");    
         }
     
     }

And in another scene this one:

 var IsSpawning : boolean = false;
 var Asteroid : Transform;
 var WaveNumber = 1;
 static var Score : int = 0;
 static var Lives : int = 3;
 static var HighScore : int = 0;
 static var EnemyCounter = 0;
 
 function Start(){
     WaveFunction(WaveNumber);
 }
 
 function Update(){
     if(Lives <= 0){
         if(Score > HighScore){
             HighScore = Score;
         }
         Application.LoadLevel("ScreenLoss");
     }
     if(EnemyCounter == 0 && !IsSpawning){
         Application.LoadLevel("ScreenShop");
     }
 }
 
 function UpdateWave(){
     WaveNumber++;
     WaveFunction(WaveNumber);
 }
 
 function WaveFunction(Wave : int){
     var EnemysInThisRound = Wave * 4;
     IsSpawning = true;
     
     for(var i = 0 ; i < EnemysInThisRound; i++){
         var position = Vector3(Random.Range(-6, 6), 12, 0);
         Instantiate(Asteroid, position, Quaternion.identity);
         yield WaitForSeconds(Random.Range(1, 1.5));
         EnemyCounter++;
     }
     IsSpawning = false;
 
 }

My question is how do I start the UpdateWave function when I press the NextWave Button in the first scene? Thank you -Nelis

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by aaronov · May 09, 2013 at 10:44 AM

You can't call functions from scripts from different scenes because only one scene can be loaded at one time. You can however have a GameObject that persists between scenes by adding the following:

 function Awake () {
     DontDestroyOnLoad (transform.gameObject);
 }

With the above code in place you should be able to place the function below in your second script and whenever the level is loaded it will call the UpdateWave() function.

 function OnLevelWasLoaded (level : int) {
     UpdateWave();
 }

You may want to add some code in there to check the level index and only call UpdateWave() on certain levels, such as "Asteroid".

Comment
Add comment · Show 5 · 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 Nelis · May 09, 2013 at 11:27 AM 0
Share

So just add these in my AsteroidScene script?

avatar image aaronov · May 09, 2013 at 11:32 AM 0
Share

Yes, you would add that to the second script in your original question.

avatar image Nelis · May 09, 2013 at 11:43 AM 0
Share

I did that. But when I'm in my AsteroidScreen and I press play and kill all the enemys, I go to the ShopScreen. If I press the 'NextWave' button there, the asteroids just spawn in the shop screen. If it could help, here is my project folder: http://www.mediafire.com/download.php?xm543lps0t2rmns

avatar image aaronov · May 09, 2013 at 11:57 AM 0
Share

It's purely up to you how it should all work I merely gave an example of what I thought you wanted to achieve. In saying that - I think you need to do what I mentioned at the end of my answer and only call UpdateWave() when level index == index of asteroid scene.

avatar image Nelis · May 09, 2013 at 02:09 PM 0
Share

I have added the index check. Also thank you for helping me.

Follow this Question

Answers Answers and Comments

13 People are following this question.

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

Related Questions

How to call a function from a script in another scene 5 Answers

Call function from other script 0 Answers

Accessing function from another script won't work 1 Answer

Similar scripts but error on one and not the other 1 Answer

Incremental game need help 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