Search Unity

Why isn't transform.position.Set working?

Discussion in 'Getting Started' started by SomeJDude, Feb 13, 2015.

  1. SomeJDude

    SomeJDude

    Joined:
    Feb 13, 2015
    Posts:
    10
    I am not new to programming or even video game development, but I am very new to Unity, and this one currently has me stumped.

    I am following the Space Shooter guide, and currently have a single GameObject in my PlayerController class

    Code (CSharp):
    1. public GameObject blastObjTemplate;
    2.     private GameObject blastObj;
    3.  
    4.     void Awake()
    5.     {
    6.         blastObj = Instantiate(blastObjTemplate, transform.position, transform.rotation) as GameObject;
    7.         blastObj.SetActive(false);
    8.     }
    I then want to have that object appear at the same point as the player after hitting the attack button, so I do this:

    Code (CSharp):
    1.         blastObj.SetActive(true);
    2.         blastObj.transform.position.Set(transform.position.x, transform.position.y, transform.position.z);
    But blastObj remains at origin. After debugging the code, I can see that the values for transform.position are as expected, but blastObj.transform.position remains the same after Set. What gives?
     
  2. protopop

    protopop

    Joined:
    May 19, 2009
    Posts:
    1,560
  3. SomeJDude

    SomeJDude

    Joined:
    Feb 13, 2015
    Posts:
    10
    Okay, that worked. Thanks! Didn't realize that transform.position returned a copy.
     
    protopop likes this.