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 /
This question was closed Dec 04, 2013 at 09:28 PM by clunk47 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Woox · Dec 17, 2010 at 12:18 AM · gameobjectarrayrandom

Array for Picking GameObjects

Hello, i have some sort of problem.

I'm trying to let my program pick a random building and spawn a big arrow above it. So that becomes the destination. So i was trying to create an array for the buildings. Another script returns an integer which is at the moment everything from 1 to 11. So what im trying to do is linking the Building their position to an integer. If someone could help me with that. That would be great. Btw i'm working in C#

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

  • Sort: 
avatar image
3
Best Answer

Answer by Statement · Dec 17, 2010 at 12:43 AM

I'm trying to let my program pick a random building and spawn a big arrow above it. So that becomes the destination.

Oh, like a way point?

So i was trying to create an array for the buildings. Another script returns an integer which is at the moment everything from 1 to 11. So what im trying to do is linking the Building their position to an integer.

So you want to reference a building by number. Well let me first point out that it can be convenient to have the numbers start from 0, since arrays start at 0. In that case you'd have the number 0 to 10 (11 unique numbers).

If someone could help me with that. That would be great. Btw i'm working in C#

I'll try!


Let's see the first part. I guess you could simply create a public array that you can modify in the editor. By the way, this removes the constraint that the size should be 11, in case you ever want to change it.

// Initially reserve 11 slots. 
// This can easily be changed from editor.
public Transform[] buildingWaypoints = new Transform[11];

Next we need a way to pick a random building, so we write a method that does the job:

public Transform GetWaypoint()
{
    int randomIndex = Random.Range(0, buildingWaypoints.Length);
    return buildingWaypoints[randomIndex];
} 

Ok, so then you wanted to spawn something on it.

public GameObject bigArrowPrefab;

public GameObject CreateWaypointArrow() { Transform waypoint = GetWaypoint(); GameObject arrowClone = Instantiate(bigArrowPrefab, waypoint.position, waypoint.rotation) as GameObject; return arrowClone; }

Then you can create an arrow by calling CreateWaypointArrow()! I added an update method that checks if mouse button is pressed and attempts to create an arrow on one of the waypoints. I hope this helps!

Here's the complete final proposed code:

public class BuildingWaypoints : MonoBehaviour { // Initially reserve 11 slots. // This can easily be changed from editor. public Transform[] buildingWaypoints = new Transform[11]; public GameObject bigArrowPrefab;

 public Transform GetWaypoint()
 {
     int randomIndex = Random.Range(0, buildingWaypoints.Length);
     return buildingWaypoints[randomIndex];
 } 

 public GameObject CreateWaypointArrow()
 {
     Transform waypoint = GetWaypoint();
     GameObject arrowClone = Instantiate(bigArrowPrefab, 
                                         waypoint.position, 
                                         waypoint.rotation) as GameObject;
     return arrowClone;
 }

 void Update()
 {
     // For quick demonstration purposes only. 
     // Press Fire1 (left mouse button) to
     // try the code out.
     if (Input.GetButtonDown("Fire1"))
     {
         CreateWaypointArrow();
     }
 }

}

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 OtsegoDoom · Feb 20, 2012 at 03:00 PM 1
Share

Although I didn't ask the question I had a similar problem and your answer solved it perfectly. Thanks!

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to randomly spawn three non repeating gameobjects from an array? 2 Answers

how to check for GameObject is null in array with random 1 Answer

how to use GameObject only once from array with random 1 Answer

C# Array with random.range help? 2 Answers

Checking if a position is occupied in 2D? 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