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 Dacardi · Mar 30, 2014 at 04:21 PM · instantiatearrayspawnontriggerenter

onTrigger spawn object C#

Hi everybody

I am pretty new to programming, and just are trying to figure it all out. Maybe someone would like to give me some advise.

I am working on a game, where a spawnPoint(emptyGameObject) is flying through a group of quads(isTrigger). The spawnPoint should spawn one of three fruits (if there is none) which should fly with the spawnpoint.

short:

  • check if flying through trigger

  • check if there is no fruit (i could not figure it out yet)

  • if both yes, spawn fruit

  • choose random fruit from array

  • let the fruit fly with spawnPoint

I got the array, but there is no spawn when I play it:


public class Fruit_Spawn_Trigger : MonoBehaviour {

 public GameObject[] myFruit;
 
 
 void OnTriggerEnter(Collider other)
 {
     Debug.Log ("Trigger active");
     int myFruit_num = Random.Range(0,2);

     if (other.gameObject.tag == "Fruit_Spawn_Trigger")
     {
         Instantiate(myFruit[myFruit_num], transform.position, transform.rotation);
     }
 }    

}


Debug Log:

  • "Trigger active" //so far so good

  • IndexOutOfRangeException: Array index is out of range. Fruit_Spawn_Trigger.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Fruit_Spawn_Trigger.cs:25) //cs:11 on my post. I deleted some (//)...whats wrong here?

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 guido123 · Mar 31, 2014 at 12:20 PM 0
Share

check if your myFruit array is filled in the editor

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Leuthil · Mar 31, 2014 at 12:30 PM

You should be able to do Random.Range(0, myFruit.length) which will always work no matter how many fruit you put in your myFruit array in the Editor. And actually @OSG Random.Range(int, int) is actually exclusive so using Random.Range(0, 2) will only return 0 or 1 and never 2. As it was discussed in this question.

As for your question about determining if there are no fruit... the best way to do this is to make a List of fruits and keep adding new fruits to the List when they are created and removing them when they are destroyed. Then you can simply do List.Count > 0 to see if there are any fruit. The reason you use a List instead of an Array is because a List can shrink/expand it's size dynamically whereas an Array has a set size when it is first initialized. Here is some code that could help you get started:

 // you must include this to have the List class available
 using System.Collections.Generic;
 
 public class Fruit_Spawn_Trigger : MonoBehaviour {
     public GameObject[] myFruit;
     private List<GameObject> aliveFruits = new List<GameObject>();
     
     void OnTriggerEnter(Collider other)
     {
         Debug.Log ("Trigger active");
         int myFruit_num = Random.Range(0,myFruit.length);
      
         if (aliveFruits.Count > 0 && other.gameObject.tag == "Fruit_Spawn_Trigger")
         {
             GameObject newFruit = Instantiate(myFruit[myFruit_num], transform.position, transform.rotation) as GameObject;
             aliveFruits.Add(newFruit);
         }
     }
 }
 


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 Dacardi · Apr 01, 2014 at 10:24 AM

Great, thank you for your answer!

It did not like "lenght", so I changed it to "Length". But there is still no spawn.

So I made the "List aliveFruits" public, to see what happens. The value rises with each passed quad but in the list no "Fruits" but "Missing(Game Object)" are added.

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 dwalker350 · Aug 23, 2015 at 01:29 AM

The "Missing(GameObject)" seems to indicate that the instantiate is returning a null pointer, which could happen if the fruit's GameObject is never set. Check the value of the fruit before spawning using Debug.Log(myFruit[myFruit_num); or Debug.Assert(myFruit[myFruit_num] != null, "Fruit #" + myFruit_num + " has not been set!");)

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

23 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

Related Questions

Instantiate 1 at a time at each transform in array 1 Answer

My Spawn Point doesn't ignore random game objects. How do I fix? 1 Answer

How Do I Add An Instantiated Object To An Array? 3 Answers

How do i prevent an object instantiating another object straight away 2 Answers

Random select from array and spawn 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