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 theshinygoombah_unity · Oct 09, 2017 at 12:26 PM · unity 5triggerfor loop

What is going on with my for loop?

I have seen dozens of issues regarding for loops here, but the scenarios don't really seem to match my issue. A lot just seem like people putting for loops in the update function. Anyways, I'm trying to set this up so that when you enter a trigger, it spawns two monsters, one at each spawn point. I can get the "Entered the Trigger" message to come up, but I don't get the "Spawn Monster" + i to show up. I expect this is really easy to fix, but I'm just out of ideas.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class WesternCottageTrigger : MonoBehaviour {

 public GameObject[] spawnPoints;
 private GameObject currentPoint;
 public GameObject monster;
 private int index;

 void Start()
 {
     Debug.Log(spawnPoints.Length);
     spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
 }

 void OnTriggerEnter(Collider other)
 {
     
     Debug.Log("Entered the Trigger");

     for (int i = 0; i < spawnPoints.Length; i++)
     {
         Debug.Log("Spawn Monster " + i);
         currentPoint = spawnPoints[i];
         monster = Instantiate(monster, currentPoint.transform.position, currentPoint.transform.rotation) as GameObject;
         
     }
     
 }

}

Below is also what I have setup in Inspector for spawnPoints and Monster

alt text

unity-screenshot.png (16.5 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

1 Reply

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

Answer by Yuvii · Oct 09, 2017 at 12:37 PM

If you don't get the '"Spawn Monster" + i ' then i guess it doesn't enter in the loop. Try to see if the spawnPoints.Length is superior to 0 with

 Debug.Log(spawnPoints.Length);

just before the loop. I see in the Start that you look at his lenght, then you define him (the other way would work better i think)

Comment
Add comment · Show 2 · 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 theshinygoombah_unity · Oct 09, 2017 at 12:49 PM 0
Share

Ok. That does answer why it is not entering the loop. Thank you. Now I have to figure out why the length is switching back to 0.

EDIT: I actually just figured it out. Got rid of spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint"); and it works perfectly now. Thank you again for the push past the mental block.

avatar image Bonfire-Boy · Oct 09, 2017 at 12:55 PM 0
Share

You're right that they should be checking the size of the array after using Find rather than before.

But, since they've set up the spawnPoints as an array in the inspector, they shouldn't need to use Find at all.

I suspect that that redundant Find call in Start() is finding nothing and so clearing the array that's been set up in the inspector (and so the Find line should be deleted).

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

156 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

Related Questions

Space Shooter Tutorial 5.3 first Asteroid being destroyed 0 Answers

Help with VR Controller Collider 1 Answer

How to Get Next One On The Que? 0 Answers

Guides or Tutorials for a Very Basic Enemy AI? [Unity 5] 1 Answer

How do I pass a parameter for trigger animations in the animator controller unity 5 file 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