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

Problem to store a gameObject

Discussion in 'Scripting' started by Vaspix, Oct 9, 2019.

  1. Vaspix

    Vaspix

    Joined:
    Jun 8, 2019
    Posts:
    54
    Hi,
    So, I'm instantiating a gameObject Model and I want it to follow my mousePos, but it just instantiates and isn't following (it's where "Follow mousePos") I tried to use curObj or Model in that statement, but nothing happened. I also tried to use just transform.position and it was working, so I think the code is correct, but the problem is that I don't know how to store this Model instantiation properly.
    Code (CSharp):
    1. if (Model != null && ifBool)
    2.         {
    3.             Instantiate(curObj = Model, mousePos, transform.rotation);
    4.             ifBool = false;
    5.         }
    6.         else if (Model != null)
    7.         {
    8.             curObj.transform.position = mousePos; // Follow mousePos
    9.         }
    So, do you know what is my mistake and how can I solve this?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Put "curObj =" before "Instantiate".
     
    Vaspix likes this.
  3. Vaspix

    Vaspix

    Joined:
    Jun 8, 2019
    Posts:
    54
    Ty, simple mistake :)