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#

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();
        }
    }
}