- Home /
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;
}
}
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
I didn't think I need to since I literally just attached the exact same script to an object on the other level.
well, we still dont know how you acces the gameobject through code, what are the conditions, is something setup in the inspector..
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;
}
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.
@SuperCrow2, Please put your scripts in the OP (opening post) not in the comment section. Thanks.
I'm not sure if this solves your problem, but there are two things in your code that could be responsible for your problem:
"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;
}
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"
}
}
Your answer
Follow this Question
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