Ok so I added code to it which I thought would get it to work but it didn’t. I tried to add a counter so the game knows how many items you collected. After collecting the item, the exit door to level 2 is supposed to bring you to level 2 when you go to it but it doesn’t. Here’s both the scripts.
Exit to level 2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ExitToLevel2 : MonoBehaviour {
private int itemcollected; //from my ItemCollected script
private int lastitemCollected = 0; //game keeps track
void OnTriggerEnter2D(Collider2D col)
{
if (itemcollected >= (lastitemCollected +1))
{
itemcollected += 1;
lastitemCollected += 1;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
item collected
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ItemCollected : MonoBehaviour
{
private int itemcollected;
private int scorevalue;
private int currentlivesvalue;
private int lastScore = 0;
private int lastitemCollected = 0; //game keeps track
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "Player")
{
Destroy(gameObject); //destroys the item
Score.scorevalue += 100; //score increases by 100 after collecting it
lastitemCollected += 1;
itemcollected += 1;
}
if (itemcollected >= (lastitemCollected + 1))
{
lastitemCollected += 1;
itemcollected += 1;
//Time.timeScale = 0;
//Destroy(col.gameObject);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
When I copy and paste the code, sometimes brackets are missing but I have the missing brackets there when I test it out/
Your comment in this line: private int itemcollected; //from my ItemCollected script
is not true. itemcollected in ExitToLevel2 script is private and local to that script. Likewise, the itemcollected in your ItemCollected script i also private and local.
In your ExitToLevel2 script you will need a reference to your ItemCollected instance, and you will need to make the itemcollected in ItemCollected public. What game object are these scripts attached to? Also, see how naming everything the same name in different methods can get confusing fast?
The problem is, when testing it out so you dont have to go to the door, it works and when I attach the itemcollected script to it it also destroys the door which then opened up a new problem which I tried to fix by using “destroy game object by tag name” which caused the character to move through the item you need to pick up instead of destroying it.
For the heck of it I tried to add a second item, and that only works with this “<=2” instead of this this “>=2” lol.
I made a new thread to try to make it less confusing.
OK, then break it down a little so we can understand how it is structured, and you’ll get better help. So you have a gameobject that the player will walk over, and this has the ItemCollected script on it? And to what gameobject is the ExitToLevel2 script attached?
Correct. And the object you walk over is the item you collect and that is supposed to make it so when you go to the Exit it goes to level 2. The exit has the ExitToLevel2 script attached to it.
Attaching the itemcollected script to the exit game object also causes the exit to be destroyed so to try to fix that I tried to do “destroy by tag name” which would only destroy the object you walk over but instead of destroying it you walk through it and it stays.
OK, so you will want your ItemCollected script attached to the game object you walk over, and the ExitToLevel2 script attached to the exit. Do you want a boolean to allow the exit to unlock when you walk over an item collected? Or do you want to count the number of items collected and unlock when a certain score has been reached?
Put this in your player script (assuming it is called PlayerController): public bool canExit = false;
ItemCollected:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using UnityEngine.SceneManagement;
public class ItemCollected : MonoBehaviour
{
//private int itemcollected;
private int scorevalue;
private int currentlivesvalue;
private int lastScore = 0;
//private int lastitemCollected = 0; //game keeps track
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "Player")
{
other.gameobject.GetComponent<PlayerController> ().canExit = true;
Destroy(gameObject); //destroys the item
Score.scorevalue += 100; //score increases by 100 after collecting it
//lastitemCollected += 1;
//itemcollected += 1;
}
/*
if (itemcollected >= (lastitemCollected + 1))
{
lastitemCollected += 1;
itemcollected += 1;
//Time.timeScale = 0;
//Destroy(col.gameObject);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
*/
}
}
Exit:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ExitToLevel2 : MonoBehaviour {
//private int itemcollected; //from my ItemCollected script
//private int lastitemCollected = 0; //game keeps track
void OnTriggerEnter2D(Collider2D col)
{
if (col.name == "Player")
{
if (col.gameobject.GetComponent<PlayerController> ().canExit)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
}
}