Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Nov 13, 2016 at 10:03 AM by mmorro1222 for the following reason:

Problem solved, see link in question for working code

avatar image
0
Question by mmorro1222 · Nov 12, 2016 at 07:55 AM · instantiatearrayboolaccessing from any scriptpickups

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

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;
     }
 }
     
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How would i use a 2D array for level generation 0 Answers

Instantiate and arrays 2 Answers

Unable to randomly instantiate prefab from array 2 Answers

How to access Instantiated GameObject's attached script's values? 1 Answer

Instantiating prefab at child (spawnlocations are arrays) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges