This line of my code is giving me an exception.
(this.gameObject).transform.position.x = GameObject.Find("First Person Controller").transform.position.x + 0.05;
This also doesn’t work.
(this.gameObject).transform.position.x = parseFloat(GameObject.Find("First Person Controller").transform.position.x) + 0.05;
However, the code worked if 0.05 was replaced with an int. Any ideas?
parseFloat() creates a float out of a string, so it shouldn’t work when you’re passing in transform.position.x, which is already a float or some other floating point var type. Have you tried casting the .05 to float explicitly (“0.05f” or “0.05F”)? I’m not too sure if it would help but it may be worth a try.
O_o
first of all:
(this.gameObject).transform.position.x
why? when you write ‘this’ you mean → this script ← which you are in now… you shouldnt use it (in this case) and you could use just gameObject, without the ‘this’ before it.
now why would you do gameObject.transform? S:
both MonoBehaviour and GameObject has the transform component as a variable. you could just write: transform, without the gameObject before it.
what you wrote there was just like writing: transform.transform.transform.transform.position.x… (legal, but never do this)
id write your script like this: (C#)
float x = GameObject.Find("First Person Controller").transform.position.x;
transform.position =new Vector3(x + 0.05f, transform.position.y, transform.position.z);
Assuming you’re using Unityscript, your first line works fine and doesn’t produce any errors. (Though “(this.gameObject)” can be replaced with just “gameObject”.)
–Eric