Search Unity

Prefabs not Instantiating in the Propper Place

Discussion in 'Scripting' started by Nitrox32, May 12, 2021.

  1. Nitrox32

    Nitrox32

    Joined:
    May 9, 2017
    Posts:
    161
    I'm creating a music game to help music students learn the notes of the musical staff. When a student selects the correct note, the note should disappear and a prefab will take its place. I'm having a problem with prefabs not spawning at the correct position of the note. The prefabs spawn at it's default transform position instead of where the note position is. It's worth mentioning that this is a 2D game. This is what I have:

    Code (CSharp):
    1.  if (button.tag == noteList[notePositionOnStaff].tag)
    2. {
    3. Instantiate(scoreAnimation, new Vector3(noteList[notePositionOnStaff].transform.position.y, noteList[notePositionOnStaff].transform.position.y, 0.0f), Quaternion.identity);
    4. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    It's also worth mentioning that line 3 above is a hairy line of code.

    Rewrite it so you an inspect the values and figure out why they are wrong.

    How to break down hairy lines of code:

    http://plbm.com/?p=248
     
  3. Nitrox32

    Nitrox32

    Joined:
    May 9, 2017
    Posts:
    161
    I figured out the problem. I had an animator that was playing an animation that set the position back to the position the animation was created. BTW thanks for the info about the hairy code. I'm always looking for ways to improve my skills.