Passing the target position from one scene to another

Hi I hope you can help me I have not used unity in a few months and have forgotten a lot of the basics, on seeing your pathfinding project I decided to get back into it as it seems very useful and well designed. I believe this answer should be fairly simple but cannot figure it out, I would like to have a main menu which contains GUI buttons depending on which GUI button is pressed I would like the target position to be changed. I have no enemies as this is not a game just a model where you can walk to different areas with the press of a button. Here is what I have tried so far but with no luck:

In the AstarAI script I modified the targetPosition to look like this

public Vector3 targetPosition = MainMenuGUI2.playerTarget;

and my MainMenuGUI2 script looks like this.

`using UnityEngine;
using System.Collections;

public class MainMenuGUI2 : MonoBehaviour {

public static Vector3 playerTarget;

void OnGUI () {
	if (GUI.Button (new Rect (10,10,150,100), "1")) {
		playerTarget = new Vector3(40, 0, 10);
		Application.LoadLevel("TestScene");
	}
	if (GUI.Button (new Rect (170,10,150,100), "2")) {
		playerTarget = new Vector3(70, 0, 20);
		Application.LoadLevel("TestScene");
	}
}

}`

This does not seem to work for me though it just goes to (0,0,0) which is what it is set at in the unity inspector.
Please any help with this will be greatly appreciated.

Since player target is simply a Vector3, it will be copied at start. The default value for a Vector3 is (0,0,0) and that is what the playerTarget variable contains at start, so only the value (0,0,0) will be copied. What you should do is one of the two:

  • Assign the myAIPath.targetPosition variable every time you click a button.
  • Modify the AIPath to take a transform object as a target (doesn’t it from the beginning?) and assign the position of that transform every time a button is pressed.