I have two scripts, target and movement. I want to give movement the transform of the gameObject that target is attached to. I can’t do this through the inspector because there is multiple of both objects and so I don’t think that’s possible. What I’m doing currently is using getComponent to access movement and there changing the transform variable it has to the transform of the gameObject.
void Start ()
{
target.position = transform.position;
script = gameObject.GetComponent<movement> ();
}
This is from the target script and I’m getting the transform of the attached gameObject and then getting access to the movement script.
void OnMouseOver ()
{
if (Input.GetMouseButtonDown (0))
{
Vector3 spawnPosition = new Vector3 (0,0,0);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (policeCar, spawnPosition, spawnRotation);
script.destination = target;
}
}
The last line is where I access the variable target in the movement script and set it as target.
public Transform destination;
private NavMeshAgent navComp;
void Start ()
{
navComp = GetComponent <NavMeshAgent>();
}
void Update ()
{
navComp.SetDestination(destination.position);
}
Finally, here is the simple movement script. So far I haven’t been able to make this work, so I was wondering if there was a simpler way of giving another script the transform?