Moving an object to a specific location

Hi Everyone,
I’m a new developer here and I’m pulling my hair out. I’m trying to do something very simple, but it’s not working and I can’t figure out why! I’m trying to simply move an object from one location to another during runtime. Below is a sample of my code where I want to move the object “target” to location 0,0,0 when a touch occurs. Does anyone know what I’m doing wrong?

Thanks!

public class BackgroundTouch : MonoBehaviour {

public GameObject target; 

// Use this for initialization
void Start () {
	target = GameObject.Find("Target");

}

// Update is called once per frame
void Update () {
	if (iPhoneInput.touchCount==0)
	{
        return;
	}
	
	target.transform.position = new Vector3(0,0,0);
	
}

}

1 Answer

1

I don’t see any problem with this as long as there’s an object called “Target” and you’re on an iPhone.

Anyway a early-exit in Update isn’t really a good idea. Usually you have multiple things happening in Update.

It would be a bit more logical this way:

if (iPhoneInput.touchCount>0)
{
    target.transform.position = new Vector3(0,0,0);
}

However your script should work as well.