Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to change the transform of a public GameObject variable in c#

Discussion in 'Scripting' started by Jonster, Feb 3, 2015.

  1. Jonster

    Jonster

    Joined:
    Mar 6, 2013
    Posts:
    2
    //in my code I created a public variable so that I could attach my seagull gameobject to the variable.

    public GameObject seagull;

    //now i want to change the transform of the seagull. I tried the following code but it is incorrect. What would be the right way to write the code below?

    seagull.transform.Translate(Vector3 (1, 0, 0) * Time.deltaTime * 5);
     
  2. meatpudding

    meatpudding

    Joined:
    Jan 28, 2015
    Posts:
    39
    seagull.transform.position = ...
     
  3. Trimadix

    Trimadix

    Joined:
    Jan 13, 2014
    Posts:
    10
    Hi Jonster,

    If you want to use translate, I would reccomend just using this code.

    Code (CSharp):
    1. seagull.transform.Translate((1, 0, 0) * Time.deltaTime * 5);
    Unless you want to use vector changes, then you would need to add a new vector 3. So just add word new before vector3.

    Also it wont change anything and itll just look bigger, but more easily readable.

    Code (CSharp):
    1. seagull.transform.Translate(1 * Time.deltaTime * 5, 0, 0);
    Since your code will be moving him only in one axis.

    Edit:
    Post your error.

    Sincerely,
    Trimadix
     
    Last edited: Feb 3, 2015
    Jonster likes this.
  4. Jonster

    Jonster

    Joined:
    Mar 6, 2013
    Posts:
    2

    Thank you very much Timadix, that solved my problem :)

    I will remember to include my error messages in any future posts I make. Thanks again.
     
  5. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    What is (1, 0, 0)? C# doesn't support tuples.