Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 TomatoOrgyLT · Jan 18, 2013 at 05:42 PM · instantiatetriggerspawnstaticvar

Spawning objects on trigger does not work as expected

Hello everyone! I created an enemy spawning system, that does not work as it should. Here are the scripts.

Spawner

  #pragma strict
       
     function OnTriggerEnter(collision : Collider) {
     
     if(collision.CompareTag("Player"))
         {
             print("YAY");
             EnemySpawn.SpawnEnemy = true;
         }
     }

EnemySpawn

 #pragma strict
 var prefab : Rigidbody;
 static var SpawnEnemy = false;
 
 function Update () {
  if(SpawnEnemy == true){
   print("Enemy has spawned");
   Instantiate(prefab,transform.position,transform.rotation);
   SpawnEnemy = false;
   }
 }

Those scripts should spawn 3 cubes(enemys) in 3 Empty GameObject positions (EnemySpawn1, EnemySpawn2, EnemySpawn3) with "EnemySpawn" script attached when a player enters a trigger. But the "EnemySpawn" script only spawns 1 cube(enemy) in 1 Empty GameObject position (EnemySpawn1) what did I do wrong? Thank you for help.

Comment
Add comment · Show 1
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 Nikhil11 · Sep 28, 2017 at 05:15 AM 0
Share

Thanks This Helped $$anonymous$$e Lot

2 Replies

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

Answer by Flynn · Jan 18, 2013 at 08:33 PM

Hello, there! First of all, I suggest that instead of using boolean signals to tell SpawnEnemy to spawn, that you instead go a little more directly: ###EnemySpawn.js:

 #pragma strict
 var prefab : Rigidbody;
 public function SpawnEnemy()//Create a public function we can call later
 {
     print("Enemy has spawned");
     Instantiate(prefab,transform.position,transform.rotation);
 }






Spawner.js

 public var eSpawn : EnemySpawn;
 #pragma strict

 function OnTriggerEnter(collision : Collider)
 {
     if(collision.CompareTag("Player"))
     {
        print("YAY");
        eSpawn.SpawnEnemy();//Call the function we defined earlier
     }
 }


But besides all of that, to actually answer your question, it seems that it only spawns one, because you only ever tell it to! Does your prefab contain more than one object in it? If not, I suggest doing this:

EnemySpawn.js

 #pragma strict
 var prefab : Rigidbody;
 public function SpawnEnemy()
 {
     for (int i = 0; i < 3; i++)//Create a for loop that repeats its self 3 times
     {
         print("Enemy has spawned");
         Instantiate(prefab,transform.position,transform.rotation);
     }
 }
Comment
Add comment · Show 8 · 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 TomatoOrgyLT · Jan 18, 2013 at 09:12 PM 0
Share

Hmmm... Now it gives me an error message Assets/Scripts/Spawner.js(8,28): BCE0020: An instance of type 'EnemySpawn' is required to access non static member 'SpawnEnemy'. what do I do?

avatar image Flynn · Jan 18, 2013 at 09:15 PM 0
Share

OH! I did not notice you were using static references... I have modified my code (added static keywords)

avatar image TomatoOrgyLT · Jan 18, 2013 at 10:14 PM 0
Share

I tried adding static keywords before and then this showed up Assets/Scripts/EnemySpawn.js(8,13): BCE0020: An instance of type 'EnemySpawn' is required to access non static member 'prefab'. This is so confusing...

avatar image Flynn · Jan 18, 2013 at 11:19 PM 0
Share

Very sorry, it's been a weird day. I've changed both scripts -- Be sure to, in the editor, set the "eSpawn" variable to the object that has the EnemySpawn script on it

avatar image TomatoOrgyLT · Jan 19, 2013 at 09:12 AM 0
Share

Great it works! I just had to change the Spawner script a bit. I added 3 "eSpawn" variables, like this.

public var eSpawn1 : EnemySpawn; public var eSpawn2 : EnemySpawn; public var eSpawn3 : EnemySpawn;

function OnTriggerEnter(collision : Collider) { if(collision.CompareTag("Player")) { print("YAY"); eSpawn1.SpawnEnemy();//Call the function we defined earlier eSpawn2.SpawnEnemy(); eSpawn3.SpawnEnemy(); } }

Thanks for the help!

Show more comments
avatar image
1

Answer by GS-Kiwibird · Jan 18, 2013 at 10:44 PM

I believe the issue is that your EnemySpawn1 instantiates an enemy, and then sets the static variable "SpawnEnemy" to false. Since it is a static variable, and that variable is now false, the other EnemySpawners will not spawn any enemies.

Try this:

Spawner

 #pragma strict
 function OnTriggerEnter(collision : Collider)
 {
     if(collision.CompareTag("Player"))
     {
         print("YAY");
         var enemySpawners : GameObject[] = GameObject.FindGameObjectsWithTag("EnemySpawner");
         for(var enemySpawn : GameObject in enemySpawners)
         {
             enemySpawn.BroadcastMessage("SpawnEnemy");
         }
     }
 }

EnemySpawn

 #pragma strict
 var enemyPrefab : GameObject;
 
 function SpawnEnemy()
 {
     print("Enemy has spawned");
     Instantiate(enemyPrefab, transform.position, transform.rotation);
 }

Make sure to your EnemySpawners are tagged as "EnemySpawner".

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 TomatoOrgyLT · Jan 19, 2013 at 09:20 AM 0
Share

This works fine too! Glad to see a different way of fixing this. Your script looks more complicated for me, but I think it has a better way to spawn the enemies. Thank you.

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

13 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

Related Questions

Spawn OnTriggerEnter 0 Answers

Instantiated (clone of) prefab, doesn't trigger another object's collider when inside it. 2 Answers

Spawn Point Problems 1 Answer

Instantiating prefab at child (spawnlocations are arrays) 2 Answers

Trigger Spawn Something At A Different Location 1 Answer


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