so I just grabbed the trial for windows today and I'm working through the 'Unity Script Reference' documentation and I'm hitting problems. I can translate objects fine, but I prefer to place them absolutely (plus translating gets tricky when you want to rotate too). my code is: Code (csharp): using UnityEngine; using System.Collections; public class CubeMove : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { transform.position.x++; } } the cube just stands there though, if I use transform.Translate() it moves, but Its bugging me why this has no effect (or error messages) any pointers would be greatly appreciated, I'm most likely just missing something horribly obvious so I'm sorry if thats the case. edit: oh yeah, its all C# of course
Well, if you want to do that, then just say transform.position.x = transform.position.x + 1; Edit: It's probably working, just increasing its position by the smallest possible denomination of unit. (Like .000000000001 or something)
when I try that it throws this at me: I am right to just drag and drop the script onto the cube object right? edit: I tried += instead and that throws the same error
That's weird.... I know it's not given me any errors when I used it that way. Could you paste in the code where you're doing it? I want to try it on my end.
wll this is the whole .cs file: Code (csharp): using UnityEngine; using System.Collections; public class CubeMove : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { //works, but not what I want //transform.Translate(1,0,0); //does nothing //transform.position.x++; //error CS1612 //transform.position.x = transform.position.x + 10; //error CS1612 //transform.position.x+= 100; } } I'm just trying little bits at a time to get to grips with it all, but some other stuff is throwing errors unexpectedly too, it might just be a corrupted install, so I'll try installing it again and seeing if I get different results. oh, as for the scene its just a blank scene with the one cube I added the script to.
Hmm.... Sorry about that, my code is all in JavaScript. I have a feeling there's some C# thing I need to do for the same result. Try creating a JavaScript with the following function: Code (csharp): function Update() { transform.position.x += 1; }
You can't do... Code (csharp): transform.position.x = transform.position.x + 1; ...in C#, since GameObject.transform is a property. What you have to do is make a temporary copy, change that copy, and then paste it back: Code (csharp): Vector3 position = transform.position; position.x = position.x + 1; transform.position = position;
well I dont quite get why it has to work like that, but seeing as it works for me that will do grand! thanks for the help guys I really apreciate it