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 delfinatarga · Nov 14, 2017 at 02:30 AM · scripting problemscene-switching

Use same script in different scenes to change level

Hello! My game has three levels, and i have a code that changes from level 1 to level 2 after killing 5 enemies. In the second level, the level should change to the third one after killing 10 enemies. I want to use the same script, but the public variables are not showing in the inspector so i can't change it. Thanks in advance for the help.

This code is attached to an empty game object:

 public class enemyCount : MonoBehaviour {
 
     public static int enemiesCount = 5;
 
     void  Update (){
         print("Enemy Count is " + enemiesCount); 
 
         if(enemiesCount <= 0)
         {
             Debug.Log("Level 2!!!");
             Application.LoadLevel ("level2");
         }
     }
 
 }


And this is attached to the bullet script:

 private void OnCollisionEnter (Collision collision){
 
         if (collision.transform.tag == "Enemy") {
 
             Destroy (collision.gameObject);
             gameObject.SetActive (false);
             Destroy (this.gameObject);
 
         }
 
         if (collision.gameObject.tag == "Enemy") {
             enemyCount.enemiesCount --; 
         }
     }
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

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Nov 14, 2017 at 02:42 AM

You can't use static variables if they should be editable in the inspector. The Inspector can only show variables that belong to this instance of the class. Static variables do not belong to a certain instance. They only exist once in the whole application.

You may want to simply use a "targetKillCount" variable which you use to initialize your static variable in Start.

I also would recommend to not use Update for this. It's way better to use a method which is called when you actually decrease the enemy count. You can check if you reached the target there.

 public class EnemyCount : MonoBehaviour
 {
     public static int enemiesCount;
     private static string nextLevel;
     public int targetCount; // set this in the inspector
     public string nextLevelName; // set the name of the next level in the inspector
     void Start()
     {
         enemiesCount = targetCount;
         nextLevel = nextLevelName;
     }
     public static void DecreaseCount()
     {
         enemiesCount--;
         print("Enemy Count is " + enemiesCount); 
         if(enemiesCount <= 0)
         {
             Debug.Log("Load level: " + nextLevel);
             Application.LoadLevel (nextLevel);
         }
     }
 }


In your other script you would use this instead:

 if (collision.gameObject.tag == "Enemy") {
     enemyCount.DecreaseCount(); 
 }
Comment
Add comment · Show 2 · 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 delfinatarga · Nov 14, 2017 at 04:02 AM 1
Share

Thank you, now i understand what my mistake was but i don't know why i get this error (sorry i'm new to Unity): An object reference is required to access non-static member `enemyCount.nextLevelName'

avatar image Bunny83 delfinatarga · Nov 15, 2017 at 01:11 AM 0
Share

Of course ^^ $$anonymous$$y mistake. Since the "DecreaseCount" method is a static method it can not use any non static variables. So we would need add also a static string variable (just like the "enemiesCount" variable) and initialize it inside Start with the non-static one. I'll edit my answer. Sorry for the mistake ^^.

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

125 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to trigger a collider to enable after a camera is inside of it. 1 Answer

Scene problem - missing Unityengine and MonoBehaviour 0 Answers

How to load a scene with GameFlow on button click ? ( XR RIG / Oculus Quest ) 0 Answers

.As u can see in the image it loads ThemeSelection scenes but doesnot show anything 0 Answers

Everytime when i start at level 3 or higher after completing it it goes back to level 2 how do i fix this? 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