- Home /
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.
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?
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
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!
Your answer
Follow this Question
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