Random select from array and spawn

Hi,

I have a very simple game where the player has to fly a helicopter from city to city. The game is in 3d. The helicopter flies over a plane with a map texture applied to it. Each time the player reaches a city, he gets the task to fly to the next city and so on.

I want to have a long list of cities that get chosen at random as the next target city to fly to. The cities are represented as a floating cube above my map. So I think I need to create an array which holds the city name and the respective coordinates of that city on my map, so that I can instantiate a new cube each time the player reaches a city.

This is where my problem starts: I have no idea if it is indeed the best way to place these in an array and how I handle the above. Here’s my code with comments on where I am having problems:

[edited my original code based on some of the comments. Same problems exist. Help appreciated!]

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

public class PlayerController : MonoBehaviour 
{
	public float speed;
	public float smooth = 2.0F;
	public GUIText countText;
	public GUIText targetCity;
	private int count;

	void Start()
	{


		List<MyCities> mycities = new List<MyCities>();
		
		mycities.Add( new MyCities("Maastricht", -5, 3, -1 )); // The transform position values of my cities are listed in the inspector as X: -5.436626 etc. but somehow the list only accepts int values?
		mycities.Add( new MyCities("Breda", -6, 3, -2));
		mycities.Add( new MyCities("Amsterdam", -2, 3, 4));


	

		SetTargetCity ();
		
		// scoring points & display on screen (works)
		count = 0;
		SetCountText ();
		
	}

	// Player Movement (works)
	void FixedUpdate ()
	{
		float moveHorizontal = Input.GetAxis("Horizontal");
		float moveVertical = Input.GetAxis("Vertical");
		
		Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

		Vector3 moveDirection= new Vector3 (moveHorizontal, 0, moveVertical);  
		if (moveDirection != Vector3.zero){
			Quaternion newRotation = Quaternion.LookRotation(moveDirection * -1);
			transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * smooth);
			
			rigidbody.AddForce(movement * speed * Time.deltaTime);
			
		}
		
	}
    // Score points by flying into city game object (works), switch off that target city game object (works), get new target city...(no idea)
	void OnTriggerEnter(Collider other)
	{
				if (other.gameObject.tag == "City") {
						other.gameObject.SetActive (false); // I used Setactive from a tutorial, but perhaps better to use destroy?
			count = count + 1;
		
			SetCountText ();
			SetTargetCity ();
				}
	}

	void SetCountText ()
	{
		countText.text = "Passengers Picked up: " + count.ToString();
	}
	

	// What do I put in this void???
	void SetTargetCity ()
	{
	randomCity = Random.Range[0,mycities.Count]; //this doesn't seem to work
	targetCity.text = "Fly to: " + randomCity.name.ToString();

		// how do I instantiate a new random city here? 
	
	
	}

}

And the other script MyCities.cs is as follows:

using UnityEngine;
using System.Collections;

public class MyCities
{
	public string name;
	public float xcor;
	public float zcor;
	public float ycor;
	
	public MyCities(string newName, float newXcor, float newZcor, float newYcor)
	{
		name = newName;
		xcor = newXcor;
		zcor = newZcor;
		ycor = newYcor;
	}
}

void SetTargetCity ()
{
var randomCity = mycities[Random.Range(0,mycities.Count-1)];
targetCity.text = "Fly to: " + randomCity.name.ToString();

    GameObject instancedCity=(GameObject)GameObject.Instantiate(cityPrefab);
    instancedCity.transform.position=new Vector3(randomCity.xcor,randomCity.ycor,randomCity.zcor);
}

Try this, it should work.
Also, you’ll need to make a prefab of your city cube. Then connect it to this script by adding another parameter in it:

public GameObject cityPrefab;

Once linked the prefab to the script, it should work.

About the float coordinates not working, have you added “f” after their values? Like, “-5.436626f”