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 SuperCrow2 · Mar 10, 2019 at 06:27 AM · sceneunity 2dgameobjects

Same script not working on next level

The same script which works on level 1 is not working on level 2. "destroy game object" is not working on level 2. Everything in the level including the player, the objects and the inspector is the same as level 1.

I do have a script that keeps the game paused on level 1 for 5 or seconds before it plays. But that cant possible mess up the other scenes right especially since starting the scene from the editor itself doesnt make it work still? But I will post both scripts.

I am using C# and unity 2d.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class ItemCollected : MonoBehaviour
 {
  
      public static int itemcollected = 0;
      private int scorevalue;
      private int currentlivesvalue;
      private int lastScore = 0;
      public int lastitemCollected = 0; //game keeps track
      void OnTriggerEnter2D(Collider2D other)
      {
          if (other.name == "Player")
          {
        
              Destroy(gameObject); //destroys the item "collects it"
          
              Score.scorevalue += 50; //score increases by 50 after collecting it
            lastitemCollected += 1;
            itemcollected += 1;
          }
          if (scorevalue >= (lastScore + 200)) //extra life every 200 points
          {
              lastScore += 200;
              currentlivesvalue += 1;
          }




using System.Collections; using System.Collections.Generic; using UnityEngine;

 //pause before level 1 starts
  
 public class countDown : MonoBehaviour
 {
  
     public GameObject CountDown;
  
  
  
   private void Start()
     {
         StartCoroutine("StartDelay");
  
    }
  
     void Update()
     {
    
      
  
     }
  
  
     IEnumerator StartDelay()
     {
        Time.timeScale = 0;
       float pauseTime = Time.realtimeSinceStartup + 3f;
         while (Time.realtimeSinceStartup < pauseTime)
             yield return 0;
       CountDown.gameObject.SetActive(false);
         Time.timeScale = 1;
  
 }
  
     }


Comment
Add comment · Show 11
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 dan_wipf · Mar 10, 2019 at 06:47 AM 0
Share

without any code provided, we can’t help realy, just speculate. please edit your question, or post get’s rejected dueto to less information FAQ

avatar image SuperCrow2 dan_wipf · Mar 10, 2019 at 03:20 PM 0
Share

I didn't think I need to since I literally just attached the exact same script to an object on the other level.

avatar image dan_wipf SuperCrow2 · Mar 10, 2019 at 03:30 PM 0
Share

well, we still dont know how you acces the gameobject through code, what are the conditions, is something setup in the inspector..

Show more comments
avatar image kamran-bigdely · Mar 10, 2019 at 03:55 PM 0
Share

could you please provide your script?

avatar image SuperCrow2 kamran-bigdely · Mar 10, 2019 at 04:03 PM 0
Share
 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 
 public class ItemCollected : $$anonymous$$onoBehaviour
 
 {
  
     public static int itemcollected = 0;
     private int scorevalue;
     private int currentlivesvalue;
     private int lastScore = 0;
     public int lastitemCollected = 0; //game keeps track
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.name == "Player")
         {
 
         
             Destroy(gameObject); //destroys the item "collects it"
           
             Score.scorevalue += 50; //score increases by 50 after collecting it
           lastitemCollected += 1;
           itemcollected += 1;
 
         }
 
 
 
         if (scorevalue >= (lastScore + 200)) //extra life every 200 points
         {
 
             lastScore += 200;
             currentlivesvalue += 1;
 
         }
avatar image zereda-games SuperCrow2 · Mar 10, 2019 at 04:15 PM 0
Share

You need a presistent data script to store your variables to move from scene to scene. look into singletons without me giving you the full answer. Next you really are not doing anything with the Private lastScore, only checking it's value here what is setting that variable?

  private int currentlivesvalue;

should be in persistent data unless you are adding lived to the global players Lives.

also destroying the object last would be a better approach.

Show more comments
avatar image kamran-bigdely · Mar 10, 2019 at 04:17 PM 0
Share

@SuperCrow2, Please put your scripts in the OP (opening post) not in the comment section. Thanks.

avatar image Sonic4080 · Mar 11, 2019 at 11:36 PM 0
Share

I'm not sure if this solves your problem, but there are two things in your code that could be responsible for your problem:

  1. "itemcollected" is a static variable, so it does not reset when the second level is loaded. So if you don't want to keep it's value in every following scene, you need to add a declaration into the Start function like this:


    void Start()
    {
        itemcollected = 0;
    }

  1. You use the Destroy methode as the first operation in your methode. I don't know exactly how Destroy() works, so my following idea could be completly wrong, but i would destroy the object as the last action, not as the first one. That would look something like this:


    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.name == "Player")
        {
            Score.scorevalue += 50; //score increases by 50 after collecting it
            lastitemCollected += 1;
            itemcollected += 1;
            if (scorevalue >= (lastScore + 200)) //extra life every 200 points
            {
                lastScore += 200;
                currentlivesvalue += 1;
            }
                Destroy(gameObject); //destroys the item "collects it"
        }
    }

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

How can i find out how much element on scene now? 1 Answer

Referencing a gameobject in another scene 1 Answer

Scene load crashes iPod 4th gen but not iPhone 4 or 3GS 0 Answers

How to get all GameObjects in Scene? 7 Answers

Load Scene Bug 0 Answers


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