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 ytwithlove · Sep 16, 2013 at 01:02 AM · c#javascriptarrays

How to randomly generate pickups

This is driving me crazy. I'm trying to convert UnityScript to C# for a tutorial I'm working on. I got pretty far until I had to do arrays - something I haven't been good at since I started programming. I keep getting an error and don't know how to fix it. Here's my code:

  using UnityEngine;
     using System.Collections;
 
 
 public class PickupController : MonoBehaviour 
 {
     //the pickup prefab assigned via the Inspector
     public GameObject pickupPrefab;
     private GameObject spawnedPickup;
     
     //the number of pickups to have around the level at any one time
     public int numberOfPickups = 2;
     
     // the ARRAY of spawnpoints that our pickup will be spawned at
     private GameObject[] spawnPointList;
  
     // array of which spawn points are currently available for spawning at
     private ArrayList spawnIndexAvailableList;
  
     // variable to hold the total number of spawn points, saves having to recalculate
     private int numberOfSpawnPoints;
 
     void Awake()
     {
         // retrieve GameObjects tagged as 'SpawnPoint' within the 'PickupSpawnPoints' GameObject which this script is a Component of
         spawnPointList = GameObject.FindGameObjectsWithTag("SpawnPoint");
         
         // retreive number of spawn points
         numberOfSpawnPoints = spawnPointList.length;
         
         // make sure number of pickups doesn't exceed number of spawn points
         if (numberOfPickups > numberOfSpawnPoints) numberOfPickups = numberOfSpawnPoints;
         
         // make all spawn points available by setting each index to true
         for (int i = 0;  i < numberOfSpawnPoints; i++) 
             {
                 spawnIndexAvailableList[i] = true;
             }
         // spawn X amount of pickups according to numberOfPickups
         for (int j = 0;  j < numberOfPickups; j++)  
             {
                 SpawnPickup();
             }
     }
     
     void SpawnPickup()
     {
         // generate a random integer to use as the index to select a spawn point from the list
         int randomSpawnIndex = Random.Range(0, numberOfSpawnPoints);
         
         // while the selected spawn index is unavailable regenerate another one
         while (!spawnIndexAvailableList[randomSpawnIndex])
             {
                 randomSpawnIndex = Random.Range(0, numberOfSpawnPoints);
             }
         // retrieve the position and rotation of the pickups spawn point
         Vector3 spawnedPickupPosition = spawnPointList[randomSpawnIndex].transform.position;
         Quaternion spawnedPickupRotation = spawnPointList[randomSpawnIndex].transform.rotation;
         
         // instantiate (create) the pickup prefab with the above position and rotation
         spawnedPickup = Instantiate(pickupPrefab, spawnedPickupPosition, spawnedPickupRotation)as GameObject;
         
         // set the spawned pickup as a child of the 'PickupSpawnPoints' gameobject that this script is a Component of
         // this is so we can use SendMessageUpwards within scripts attached to the pickupPrefab to call functions within this script
         spawnedPickup.transform.parent = spawnPointList[randomSpawnIndex].transform;
         
         // set the name of the pickup as its index
         spawnedPickup.name = randomSpawnIndex.ToString();
         
         // make the spawn index unavailable to prevent another pickup being spawned in this position
         spawnIndexAvailableList[randomSpawnIndex] = false;
 
     }
 
 }

This is what I have up to this point in the tutorial. Hopefully once I get the arrays fixed I can get the rest of the tutorial done tonight. Any suggestions?

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 getyour411 · Sep 16, 2013 at 01:09 AM 0
Share

What's the error?

3 Replies

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

Answer by supernat · Sep 16, 2013 at 01:21 AM

 private ArrayList spawnIndexAvailableList;

You need to initialize the ArrayList before you can use it. Everything that is not a native type (int, float, bool, etc) must be constructed in C# (technically I think native types are as well, but the compiler handles that for you...I think):

 private ArrayList spawnIndexAvailableList = new ArrayList();

ArrayList also needs to have items added to it using spawnIndexAvailableList.Add() if you construct it empty. If you know the size of the ArrayList ahead of time, you can create it as follows (I'm trying to tie it into your code):

 ArrayList spawnIndexAvailableList = new ArrayList(numberOfSpawnPoints);

This code line would go in your Awake() method right after you set numberOfSpawnPoints.

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 ytwithlove · Sep 16, 2013 at 02:28 AM 0
Share

Okay! Great! That definitely cleared a few of my errors. The final error I'm getting says "The !' operator cannot be applied to operand of type object'" for my while statement in SpawnPickup(). I thought that is how you were supposed to set up a while statement to randomly generate the prefab. I'm guessing I'm missing something in my syntax?

avatar image supernat · Sep 16, 2013 at 03:57 AM 0
Share

I may be wrong on this, but the compiler is probably trying to deter$$anonymous$$e the type of the element based on how your code is structured. So if you say "spawnIndexAvailableList[x] = true", it knows it is a boolean, but (!...) I guess can be used for non-bool types. Try "while (spawnIndexAvailableList[x] == false)" ins$$anonymous$$d. It could also be some quirky operator order of precedence issue, so "while (!(spawnIndexAvailableList[x]))" may also solve the problem.

avatar image ytwithlove · Sep 16, 2013 at 07:36 PM 0
Share

I tried "while (spawnIndexAvailableList[x] == false)" and I now get the error: "Operator ==' cannot be applied to operands of type object' and bool'" Using "while (!(spawnIndexAvailableList[x]))", I get the error: "The !' operator cannot be applied to operand of type `object'" like before.

I'm wondering if there is a better way to write this that doesn't use the while loop but I really can't think of anything at the moment. $$anonymous$$aybe I just need to walk away for a little bit.

Thanks for you help though supernat! I appreciate! :)

avatar image supernat · Sep 17, 2013 at 03:02 AM 0
Share

Thanks for Josh's clarification in his answer on this one. I haven't really worked with ArrayList much. It would probably make more sense to replace the ArrayList with just a simple boolean array. You aren't really using ArrayList's full potential of storing different types and having dynamic insertion and removal features.

 private bool[] spawnIndexAvailableList;
 ...
 // ...down in your Awake method...
 // retreive number of spawn points
 numberOfSpawnPoints = spawnPointList.Length;
 spawnIndexAvailableList = new bool[numberOfSpawnPoints];

Now, you don't need to cast anything, because you're working with a native array. The only obvious thing is you can't easily remove or insert new items into the array like you can with an ArrayList, so you need to consider that.

avatar image ytwithlove · Sep 17, 2013 at 08:24 PM 0
Share

Well I tried the above method and now I'm getting a new error: "Assets/Scripts/PickupController.cs(28,54): error CS1061: Type UnityEngine.GameObject[]' does not contain a definition for length' and no extension method length' of type UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?)"

Don't even know what to do with this. Any suggestions?

Show more comments
avatar image
0

Answer by josh-horsley · Sep 16, 2013 at 09:31 PM

With an ArrayList, it only knows that it's holding objects but doesn't know exactly what it's holding. If your spawnIndexAvailableList will only ever contain a list of boolean values, try explicitly casting to a bool in your while loop. Something like this:

  // while the selected spawn index is unavailable regenerate another one
 while (!(bool)spawnIndexAvailableList[randomSpawnIndex])
 {
 randomSpawnIndex = Random.Range(0, numberOfSpawnPoints);
 }
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 ytwithlove · Sep 17, 2013 at 12:50 AM 0
Share

That helped greatly! Thank you! Now I'm getting a warning for my number of spawnpoints: "Assets/Scripts/PickupController.cs(21,21): warning CS0649: Field PickupController.numberOfSpawnPoints' is never assigned to, and will always have its default value 0'"

When I run the game now I don't see any of my pickup models. I have the spawnpoints placed right in front of the player so they would show in the camera view.

Does this have to do with numberOfSpawnPoints not being initialized at start?

avatar image josh-horsley · Sep 17, 2013 at 02:30 AM 0
Share

Hmm...I copied your code and opened it in $$anonymous$$onoDevelop. The only thing I saw with a warning was the ArrayList, spawnIndexAvailableList, which I think supernat already helped with. I would compare your current code with what you have posted here, because numberOfSpawnPoints looks fine from what I see.

avatar image
0

Answer by YoungDeveloper · Sep 18, 2013 at 11:03 AM

Check out this topic, maybe it will be helpful. http://answers.unity3d.com/questions/534933/random-spawn-random-prefab.html

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 ytwithlove · Sep 20, 2013 at 01:04 AM 0
Share

Thanks! I'll take a look and see where it takes me. I think I may have to start over from scratch and see where the problem lies.

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

18 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

if statement only ever triggering the first condition even if it's false and the second one is true 0 Answers

Using a Parameterized arraylist (C#)??? 1 Answer

Best way to keep track of objects on a 3D Grid? 2 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