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 /
  • Help Room /
avatar image
0
Question by vonbetelgeuse · Jul 14, 2016 at 10:53 PM · c#updatespawninginvokerepeatingspawning-enemies

Improving script which Spawns enemies up to a required number

This script I have written spawns fish up to a required number and upholds that number of fish in the scene, respawning more up to the number when fish get destroyed.

Could anyone offer some improvements on it? How can I make it run faster?

 using UnityEngine;
 using System.Collections;
 
 public class randomFish : MonoBehaviour {
 
     public GameObject[] fish; // this is where all the spawned fish are held
     public int maxAmountOfFish; // wanted maximum amount of fish in scene
     private int amount; // amount of fish in GameObject[] fish
     private Vector3 spawnPoint; // spawn point of fish
     private int randomFishNumber; // number of prefab selected to be cloned at random
     public GameObject fishClone;
 
 
     void Update ()
     {
         GameObject[] fish = GameObject.FindGameObjectsWithTag ("fish");
         amount = fish.Length; // the amount of fish in the scene is the length of the array called "fish"
 
         if (amount != maxAmountOfFish) {
             InvokeRepeating ("Spawn", 1, 60f);
         }
     }
 
 
     void Spawn ()
     {
             spawnPoint.x = Random.Range (-2.5f, -5.0f); // spawn points to spawn from
             spawnPoint.y = Random.Range (-0.8f, -0.5f);
             spawnPoint.z = Random.Range (-5.0f, 5.0f);
 
             randomFishNumber = Random.Range (1, 6); // random fish 1-5 (6 excluded)
             fishClone = Resources.Load ("FishPrefabs/SALMON" + randomFishNumber) as GameObject;
             Debug.Log (fishClone);
             fishClone = Instantiate (fishClone, spawnPoint, Quaternion.identity) as GameObject;
             fishClone.tag = "fish";
             CancelInvoke();
         }
 }
 

Comment
Add comment · Show 4
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 Akzwar · Jul 14, 2016 at 11:30 PM 0
Share

If you are helding all fish in global array, why do you need fish array in Update()?

avatar image vonbetelgeuse Akzwar · Jul 15, 2016 at 10:51 AM 0
Share

I need to update the array to check how many fish have been spawned into the array, to know how many fish are in the scene so the constant maximum number is upheld if a couple get destroyed, although there may be a better way of doing this?

avatar image jdean300 · Jul 15, 2016 at 12:59 AM 1
Share

Do you need to make it run faster? While there are some $$anonymous$$or ways to improve the performance (cache the loaded prefabs, maintain an array of fish rather than finding them by tag every frame), there is not much you can do here. Until you have profiled your game and found out that this code is causing a problem, don't waste too much time trying to make it faster.

avatar image vonbetelgeuse jdean300 · Jul 15, 2016 at 10:54 AM 0
Share

I'm just trying to learn how to make my scripting better as a beginner, I'm sure I'm doing some elementary mistakes in the eyes of the proficient so I ask :) Caching loaded prefabs isn't something I've looked into (and probably should because I don't know what it does) so thank you for pointing that out to me!

I'm not sure I can maintain an array without update as I'm destroying and spawning fish tagged "fish"? $$anonymous$$y understanding is if I don't check it in update then it won't check the number, although I could put this under an InvokeRepeating and check every three seconds perhaps?

0 Replies

· Add your reply
  • Sort: 

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

191 People are following this question.

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

Related Questions

Start() and Update() execution issue 6 Answers

How can I instantiate a object on the world coordinates 1 Answer

Applying Scripts held in a list to a game object 1 Answer

Items spawn and will not stop spawning. 2 Answers

Message Centre Does Not Display 0 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