Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 DroidifyDevs · Jul 19, 2015 at 01:15 AM · unity 2dperformancespawnlaglow fps

Serious performance issues

So I am making a game where I have a player that moves on the X axis, and catches objects that fall on him to earn points. Here is the script I use to spawn the objects:

 using UnityEngine;
 using System.Collections;
 
 public class Spawner : MonoBehaviour
 {
     private GameObject[] locationsToSpawn;
     private float counter = 0;
     [SerializeField]
     string[] listOfPossibleTags;
     [SerializeField]
     GameObject[] objectToSpawn;
     [SerializeField]
     float timeBetweenSpawns = 3.0f;
 
     void Start()
     {
         locationsToSpawn = GameObject.FindGameObjectsWithTag("SpawnLocation");
     }
     void Update()
     {
         counter += Time.deltaTime;
         if (counter > timeBetweenSpawns)
         {
             GameObject spawnedObject;
             spawnedObject = Instantiate(objectToSpawn[Random.Range(0, objectToSpawn.Length)], locationsToSpawn[Random.Range(0, locationsToSpawn.Length)].transform.position, Quaternion.identity) as GameObject;
             spawnedObject.gameObject.tag = listOfPossibleTags[Random.Range(0, listOfPossibleTags.Length)];
             counter = 0;
         }
     }
 }


So with the code above, the objects are spawned above the player and begin falling on him. The player moves and picks up the falling objects to earn points. Here is my script that counts the points:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class POINTS : MonoBehaviour
 {
 
     public Text countText;
     public Text winText;
 
 
     private Rigidbody rb;
     private int count;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         count = 0;
         SetCountText();
         winText.text = "";
     }
 
 void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Pickup"))
         {
             other.gameObject.SetActive(false);
             count = count + 100;
         }
             SetCountText();
             if (other.gameObject.CompareTag("TestSolid"))
             {
                 other.gameObject.SetActive(false);
                 count = count - 300;
                 SetCountText();
             
             }
     }
 
     void SetCountText()
     {
         countText.text = "Score: " + count.ToString();
         if (count >= 1200)
         {
             winText.text = "Congrats! To play again press <";
         }
     }
 }


Looks good in my opinion. Only problem is, after the player picks up a few objects, the score starts sky rocketing. So I have it set up so every object is +100 points, but after picking up a few objects the score starts adding a lot more points than +100, so after picking up 5 objects I'm already at 1,500 points!!! And once the score skyrockets past 23,000 points (about 10-15 objects), then the game starts lagging and eventually crashes Unity. What is wrong with my game? I don't see why I'm having such performance issues and why my point counter isn't working right.

Comment
Add comment · Show 3
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 meat5000 ♦ · Jul 19, 2015 at 01:18 PM 0
Share

Basic $$anonymous$$aths Application required..

100 + 200 + 300 + 400 + 500 = 1500

in 5 steps as you describe.

You clearly haven't written

count += count + 100;

by mistake so one can only conclude that your other pickups are somehow still registering or compounding somewhere else.

If this script is on the Player and not on the Pickups...

Are you duplicating your player?

avatar image maccabbe · Jul 19, 2015 at 02:24 PM 0
Share

Well part of the problem is that you are just disabling gameobjects after they are picked up. This might be causing some problems since "Trigger events will be sent to disabled $$anonymous$$onoBehaviours, to allow enabling Behaviours in response to collisions"

http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter.html

avatar image DroidifyDevs · Jul 19, 2015 at 02:56 PM 0
Share

So I added count += 100; and now my score goes 0,100,300,1500,12700. Obviously didn't help. I deleted the old points.cs and renamed it to points1.cs and attached it only to my player. And I have only 1 player. I suspect my spawn script has something to do with it as if I just put all the objects in the scene without it the counter works great. And even if I change my code to this, the objects are destroyed, but the game is unplayable after about 20 objects.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class POINTS1 : $$anonymous$$onoBehaviour
 {
 
     public Text countText;
     public Text winText;
 
 
     private Rigidbody rb;
     private int count;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         count = 0;
         SetCountText();
         winText.text = "";
     }
 
     void SetCountText()
     {
         countText.text = "Score: " + count.ToString();
         if (count >= 1200)
         {
             winText.text = "Congrats! To play again press <";
         }
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Pickup"))
         {
             other.gameObject.SetActive(false);
             count = count + 100;
         }
 
         Destroy(other.gameObject);
 
     }
 }


Thanks for the help!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Editor incredibly slow, weird profiler output. 1 Answer

Low FPS in very simple Android game 1 Answer

performance question about particles and instantiating 1 Answer

Unity to android (game very laggy) 1 Answer

Unity performance issues. 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