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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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,550
  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.