How can I find the x and y axis of a gameObject in script, so I can check whether the object is above or below a certain height? for example if the gameObject drops below 100 meters, it triggers something.
This worked in previous games I worked on but now I use it and this is the error I get:
error CS1612: Cannot modify the return value of āTransform.positionā because it is not a variable
This is my code:
using UnityEngine;
public class RandomizeMissleSpawns : MonoBehaviour
{
// Update is called once per frame
void Update()
{
transform.position.x = Random.Range(-11, 12);
Debug.Log(transform.position.x);
}
}
It works! Thank you! and just so you know, I wanted it to go like crazy because I was moving a object to determine Missle Spawning. I also learned how to give a value to a Vector3 in code! Thanks!
By the way, I find myself doing similar things with Vectors often enough that I wrote some simple extension methods that let you quickly get a Vector3 that is the same as some other Vector3, but with one dimension that is different:
public static Vector3 WithX(this Vector3 vector, float x) {
return new Vector3(x, vector.y, vector.z);
}
public static Vector3 WithY(this Vector3 vector, float y) {
return new Vector3(vector.x, y, vector.z);
}
public static Vector3 WithZ(this Vector3 vector, float z) {
return new Vector3(vector.x, vector.y, z);
}
With these extensions your code could be a little simpler:transform.position = transform.position.WithX(Random.Range(-11, 12));