[working script in a different question][1]
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*)*
{
//set that the item has been collected in the array
manifest.itemsCollected = 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?
_*[1]: Solved - only spawn item pickups if not previously picked up - Questions & Answers - Unity Discussions