Search Unity

N00b's moving issue

Discussion in 'Scripting' started by SophieHoulden, Mar 19, 2009.

  1. SophieHoulden

    SophieHoulden

    Joined:
    Mar 19, 2009
    Posts:
    20
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CubeMove : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.    
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.         transform.position.x++;
    14.     }
    15. }
    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 :)
     
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    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)
     
  3. SophieHoulden

    SophieHoulden

    Joined:
    Mar 19, 2009
    Posts:
    20
    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 :(
     
  4. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Don't capitalize the t. Keep it the lowercase transform.
     
  5. SophieHoulden

    SophieHoulden

    Joined:
    Mar 19, 2009
    Posts:
    20
    its definately lower case, I checked I wether it was a class or not before I typed it
     
  6. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    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.
     
  7. SophieHoulden

    SophieHoulden

    Joined:
    Mar 19, 2009
    Posts:
    20
    wll this is the whole .cs file:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CubeMove : MonoBehaviour {
    5.  
    6.    // Use this for initialization
    7.    void Start () {
    8.    
    9.    }
    10.    
    11.    // Update is called once per frame
    12.    void Update () {
    13.        //works, but not what I want
    14.        //transform.Translate(1,0,0);
    15.        
    16.         //does nothing
    17.         //transform.position.x++;
    18.        
    19.        //error CS1612
    20.         //transform.position.x = transform.position.x + 10;
    21.        
    22.        //error CS1612
    23.         //transform.position.x+= 100;
    24.    }
    25. }
    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.
     
  8. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    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):
    1. function Update()
    2. {
    3.    transform.position.x += 1;
    4. }
     
  9. tolm

    tolm

    Joined:
    Feb 4, 2008
    Posts:
    61
    You can't do...
    Code (csharp):
    1. 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):
    1. Vector3 position = transform.position;
    2. position.x = position.x + 1;
    3. transform.position = position;
     
  10. SophieHoulden

    SophieHoulden

    Joined:
    Mar 19, 2009
    Posts:
    20
    well I dont quite get why it has to work like that, but seeing as it works for me that will do grand! :D

    thanks for the help guys I really apreciate it :)
     
  11. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Heh, sorry I couldn't be of more help.

    It is nice to see other students on here, though.