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 /
avatar image
0
Question by FLy1nRabBit · Apr 17, 2016 at 11:43 AM · 2dspritearraysspawninggeneration

Sprite Spawning System with Array Help?

Basically I've got three arrays, each creates its own sprite with a spawn location and a prefab I've assigned. The prefabs are little rain sprites that float around on my 2D map. Once the sprite leaves the bounds of the camera it deletes itself.

I wanted to know if there was a way I could perhaps add a loop or some kind of concatenation system that could run through the array and create a new sprite once the old one has been deleted?

alt text

Spawn Script:

 public Transform[] spawnLocations;
 public GameObject[] SpawnPrefab;
 public GameObject[] SpawnClone;

 void Start()    {

     spawnIntelligence();
 }

 void spawnIntelligence() {

     SpawnClone[0] = Instantiate(SpawnPrefab[0], spawnLocations[0].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
     SpawnClone[1] = Instantiate(SpawnPrefab[1], spawnLocations[1].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
     SpawnClone[2] = Instantiate(SpawnPrefab[2], spawnLocations[2].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;

 }

Deletion Script:

 void Start () {

 }

 void Update () {
     Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
     if (screenPosition.y > Screen.height || screenPosition.y < 0) {
         Destroy(this.gameObject);
     }

     if (screenPosition.x > Screen.width || screenPosition.x < 0) {
         Destroy(this.gameObject);


     }    
 }

Thanks in advance. I'm still pretty new to Unity and have since switched from Java over to C#. Any help or insight would be appreciated by my friend and I!

357b2cfbc4518c54156e4e43a4e3ef3c.png (28.4 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TBruce · Apr 17, 2016 at 06:19 PM

@FLy1nRabBit

Going by your snapshop and the sample script it looks like even though you have a SpawnPrefab array of 3 elements there is only one prefab. I suggest the following which allows you to have just the one public prefab property. It uses a List instead of an Array which is more effcient. It also has functionality to control your spawned ites by way of activating/deactivating the spawned items (which is the better way to go) or by adding and removing of spawned items. It also looks like you are destroying the game object in another script, so I made it so that this can be handled in the TempSpawner class.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TempSpawner : MonoBehaviour
 {
     public GameObject SpawnPrefab;
     
     public List<Transform> spawnLocations = new List<Transform>();
     public List<GameObject> SpawnClone = new List<GameObject>();
     
     void Start()
     {
         if (SpawnPrefab != null)
         {
             if (spawnLocations.Count > 0)
             {
                 spawnIntelligence();
             }
             else
             {
                 Debug.LogError("There are no Spawn Locations. You need add at least one Spawn Location in the inspector");
             }
         }
         else
         {
             Debug.LogError("Spawn Prefab is null. You need to set the Spawn Prefab property in the inspector");
         }
     }
 
     void spawnIntelligence()
     {
         for (int i = 0; i < spawnLocations.Count; i++)
         {
             AddItem(i);
         }
     }
 
     public void DisableItem(int item)
     {
         if ((item < spawnLocations.Count) && (SpawnClone[item] != null))
         {
             SpawnClone[item].SetActive(false);
         }
     }
 
     public void ReenableItem(int item)
     {
         if ((item < spawnLocations.Count) && (SpawnClone[item] != null))
         {
             SpawnClone[item].transform.position = spawnLocations[item].transform.position;
             SpawnClone[item].SetActive(true);
         }
     }
 
     public void RemoveItem(int item)
     {
         if ((item < spawnLocations.Count) && (SpawnClone[item] != null))
         {
             Destroy(SpawnClone[item]);
             SpawnClone.RemoveAt(item);
         }
     }
 
     public void AddItem(int item)
     {
         if (item < spawnLocations.Count)
         {
             SpawnClone.Add((GameObject)Instantiate(SpawnPrefab, spawnLocations[item].transform.position, Quaternion.Identity));
         }
     }
 
     void Update ()
     {
         for (int i = 0; i < spawnLocations.Count; i++)
         {
             Vector2 screenPosition = Camera.main.WorldToScreenPoint(SpawnClone[i].transform.position);
             if ((screenPosition.y > Screen.height) || (screenPosition.y < 0) ||
                 (screenPosition.x > Screen.width) || (screenPosition.x < 0))
             {
                 DisableItem(i);
             }    
         }
     }
 }
 

Comment
Add comment · Show 1 · 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
avatar image FLy1nRabBit · Apr 18, 2016 at 03:24 PM 0
Share

Thanks for the help from everybody! Using Javascript for awhile before switching to C# I had no idea about lists. This worked great, I'm going to be adding a timer to the update function which shouldn't be to big of a hassle.

Thanks again, my partner and I appreciate it!

avatar image
1

Answer by Inok · Apr 17, 2016 at 11:56 AM

You need to switch from build-in array to List. It give you ability to add and delete elements:

 List <GameObject> Spawn_Prefab_List = new List<GameObject>();
 Spawn_Prefab_List.Add(GO);
 Spawn_Prefab_List.Remove(GO);

Can't answer in more detail, sorry.

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
avatar image
0

Answer by kumarc123 · Apr 17, 2016 at 12:12 PM

Use Lists. You can add, delete an item into a List easily. But one thing I understood from your question is you're creating rain sprites and deleting (destroying) it. Why not use object pool system. You create some sprites initially, re-use if needed, and disable them whenever you want to delete instead of destroying.

You can also use particles for your rain kind of scene.

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Disfigured sprite after build. 0 Answers

A few questions about spawning and changing sprites! (2D) 0 Answers

Draw/generate shape as 2D sprite? 0 Answers

Asset store categories - How to determine best fit for my asset 0 Answers

how do i make an object always face the player? 5 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