How do I get objects position and use it as a target variable in csharp?


I have a script that allows an object to move and rotate to a specific target, via dragging the desired object from the hierarchy onto the target slot in the inspector view. So what I want to do is to set the target without having to drop it on a slot manually, but I can’t figure out how to do that.

This is the script:

using UnityEngine;
using System.Collections;

public class MoveToTarget : MonoBehaviour { 

public Transform target;      
public float speedRotation = 5;   
public float movingSpeed = 80;    
public float minimalDistance = 2;    
public float headSpeedRotation = 5;    
private Quaternion initialRotation;

// Use this for initialization
void Start () {

// Update is called once per frame
void Update ()

public void RotateTo(Vector3 targetPosition)

		Quaternion destRotation;

		Vector3 relativePos;

		Vector3 tap;

		Vector3 trp;

		tap = targetPosition;

		trp = transform.position;

		tap.y = 0;

		trp.y = 0;

		relativePos = tap - trp;

          destRotation = Quaternion.LookRotation(relativePos);

		transform.rotation = Quaternion.Slerp(transform.rotation,destRotation,speedRotation *Time.deltaTime);

	public void Walk(Vector3 targetPosition){

  	Vector3 velocity;

  	Vector3 moveDirection = transform.TransformDirection(Vector3.forward);

  	Vector3 delta = targetPosition - transform.position;

		if (delta.magnitude > minimalDistance)
  	    animation["Walk"].speed = 1.4f;

		    velocity = moveDirection.normalized * movingSpeed * Time.deltaTime;

		    velocity =;

   		rigidbody.velocity = velocity;

My question is: How do I set the variable target.position in the update function equal to, let’s say an object with the tag “Enemy”?

I’d be glad for any advice.

Usually you set the target variable at Start using FindWithTag:

void Start(){
    target = GameObject.FindWithTag("Enemy").transform;

You should not use any Find family instructions inside Update because they are all slow operations, and will impact the frame rate.

Thanks alot aldonaletto! That works!

Another Question: I’m using the downloadable headlookcontroller script. In it, there’s a Vector3 variable called target. That’s the variable that refers to the object in worldspace, that the character will look at. Is there any way to call this variable in the script above and make it equal to the target variable that I we have set to GameObject “Enemy”? I tried the following:

First I changed the target variable in the headlookcontroller script from “public Vector3 target;” to “public static Vector3 target;” so I can access it in the MoveToTarget script.

Then in the MoveToTarget script:

void Start() {
target = GameObject.FindWithTag("Enemy").transform; = target.position;

This doesn’t work.
My goal is, to make the headlookcontroller Vector3 target equal to the MoveToTarget target after we set it equal to the object with tag “Enemy”, but I have no Idea how to accomplish that… (I tried many combinations, but everything I get is a lot of parse errors!)

Please help! :frowning: