- Home /
Problem solved, see link in question for working code
setting array elements mid-game in an array created/held by a different script
working script in a different question
So I'm currently working on making it so that when a scene is reloaded due to the player dying, items that the player collected before dying are not respawned. I had another topic on this, however I think I've narrowed down the problem and have a more specific question. Basically, items are spawned via spawn points that run a spawner script. In the spawner script I check whether or not the object was previously collected by referencing arrays held in another script with Don'tDestroyOnLoad(also a check if this is not the first time the scene is loaded, then destroy), if not I instantiate the object. The arrays used are items[] (holds a list of item spawn points set via editor for each scene) and itemsCollected[] (a bool array)
The problem is in the PickupManager script, which is attached to the instantiated objects and handles what happens when the object is collected. This is where the itemsCollected[] array should be set, however watching the array while the game is running, I noticed the array's elements were not being set to true. Therefore, the items would still spawn after the player collects them and dies to reload the scene.
this is the code in which I instantiate the object (only consider the section regarding Coins)
void Spawn(GameObject item)
{
if (item.tag == "Coin")
{
Instantiate(coinPickup, item.transform.position, Quaternion.identity, item.transform);
}
if (item.tag == "Health")
{
Instantiate(healthPickup, item.transform.position, Quaternion.identity);
}
if (item.tag == "Life")
{
Instantiate(lifePickup, item.transform.position, Quaternion.identity);
}
}
this is the PickupManager script, I'm trying to set itemsCollected() in OnTriggerEnter2d()
using UnityEngine;
using System.Collections;
public class PickupManager : MonoBehaviour
{
public PlayerManager player;
public AudioSource sound;
public ItemManifest manifest;
private bool collected;
// Use this for initialization
void Awake()
{
player = GameObject.FindWithTag("Player").GetComponent<PlayerManager>();
sound = GetComponent<AudioSource>();
collected = false;
//preparing for item manifest recording
manifest = GameObject.FindWithTag("Manifest").GetComponent<ItemManifest>();
}
// Update is called once per frame
void Update()
{
if (!sound.isPlaying && collected == true)
Destroy(gameObject);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player" && collected == false)
{
collected = true; //used to determine when to Destroy the gameObject
//search through item array to find this item
for (int i = 0; i < manifest.items.Length; i++)
{
if (this.gameObject.transform.parent == manifest.items[i])
{
//set that the item has been collected in the array
manifest.itemsCollected[i] = true;
}
}
...
}
}
}
I've tried to remove the if statement(expecting every value in itemsCollected to be set to true on pickup) to see if that was the problem, it wasn't, every element is still false. Is it not possible to directly modify an array located in a different script?
Answer by mmorro1222 · Nov 13, 2016 at 10:24 PM
Through much effort I've solved my problem (this one at least). Dumping scripts here if anyone's interested. spawner script:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class PickupSpawner : MonoBehaviour
{
public GameObject coinPickup, healthPickup, lifePickup;
private Object clone;
public ItemManifest manifest;
void Awake()
{
if (PlayerPrefs.GetString("LastScene") == SceneManager.GetActiveScene().name && PlayerPrefs.HasKey(this.name))
{
PlayerPrefs.DeleteKey(this.name);
Destroy(gameObject);
}
else
{
PlayerPrefs.SetInt(this.name, 1);
}
manifest = GetComponentInParent<ItemManifest>();
StartCoroutine(startSpawn());
}
//IEnumerator to delay spawning items so ItemManifest has time to set collected array size.
IEnumerator startSpawn()
{
yield return new WaitForSeconds(0.2f);
//spawn items if not collected
for (int i = 0; i < manifest.items.Length; i++)
{
if (manifest.itemsCollected[i] == false)
{
Spawn(manifest.items[i]);
}
}
}
void Spawn(GameObject item)
{
if (item.tag == "HealthSpawn")
{
clone = Instantiate(healthPickup, item.transform.position, Quaternion.identity, item.transform);
if (this.transform.childCount > 1)
{
Destroy(clone);
}
}
if (item.tag == "CoinSpawn")
{
clone = Instantiate(coinPickup, item.transform.position, Quaternion.identity, item.transform);
if (this.transform.childCount > 1)
{
Destroy(clone);
}
}
if (item.tag == "LifeSpawn")
{
clone = Instantiate(lifePickup, item.transform.position, Quaternion.identity, item.transform);
if (this.transform.childCount > 1)
{
Destroy(clone);
}
}
}
}
pickupManager script
using UnityEngine;
using System.Collections;
public class PickupManager : MonoBehaviour
{
public PlayerManager player;
public AudioSource sound;
public ItemManifest manifest;
private bool collected;
// Use this for initialization
void Awake()
{
player = GameObject.FindWithTag("Player").GetComponent<PlayerManager>();
sound = GetComponent<AudioSource>();
collected = false;
manifest = GameObject.Find("ItemManifest").GetComponent<ItemManifest>();
}
// Update is called once per frame
void Update()
{
if (!sound.isPlaying && collected == true)
Destroy(gameObject);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player" && collected == false)
{
collected = true; //used to determine when to Destroy the gameObject
//search through item array to find this item
for (int i = 0; i < manifest.items.Length; i++)
{
if (this.transform.parent.gameObject == manifest.items[i])
{
manifest.setCollected(i);
}
}
//if-else block to determine effect of item
if (this.gameObject.tag == "Coin")
{
player.score += 1;
}
else if (this.gameObject.tag == "Health")
{
player.hearts = 3;
}
else if (this.gameObject.tag == "Life")
{
player.lives += 1;
}
//play coin/health/life pickup sound
sound.Play();
//remove game object from view, later destroyed when sound is not playing via update function
this.transform.localScale = new Vector3(0, 0, 0);
}
}
}
itemManifest script
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class ItemManifest : MonoBehaviour
{
public GameObject[] items;
public bool[] itemsCollected;
// Use this for initialization
void Awake()
{
if (GameObject.FindGameObjectsWithTag("Manifest").Length < 1)
{
DontDestroyOnLoad(this);
//PlayerPrefs.SetString("LastScene", SceneManager.GetActiveScene().name);
}
this.tag = "Manifest";
if (PlayerPrefs.GetString("LastScene") == SceneManager.GetActiveScene().name && GameObject.FindGameObjectsWithTag("Manifest").Length > 1)
{
Destroy(gameObject);
}
PlayerPrefs.SetString("LastScene", SceneManager.GetActiveScene().name);
itemsCollected = new bool[items.Length];
}
void Update()
{
if (PlayerPrefs.GetString("LastScene") != SceneManager.GetActiveScene().name)
{
Destroy(gameObject);
}
}
public void setCollected(int i)
{
itemsCollected[i] = true;
}
}