Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiating error?

Discussion in 'Scripting' started by Avi3ator, Nov 2, 2017.

  1. Avi3ator

    Avi3ator

    Joined:
    Feb 29, 2016
    Posts:
    64
    Im instantiating an object when the enemy dies, but the object im instantiating spawns like 2(meters?) down? this is the instantiate code:

    Code (CSharp):
    1. PickupItem instance = Instantiate(pickupItem, transform.position,Quaternion.identity);
    2.     instance.ItemDrop = item;
    picture:
    https://gyazo.com/922afd7fb9504f54fd83b08adced0341
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    What is pickupItem?

    If it's a prefab, how is the object set up?
     
  3. Avi3ator

    Avi3ator

    Joined:
    Feb 29, 2016
    Posts:
    64
    its a prefab with this scrip on it:

    Code (CSharp):
    1.     public Item ItemDrop { get; set; }
    2.  
    3.     public override void Interact()
    4.     {
    5.         InventoryController.Instance.GiveItem(ItemDrop);
    6.         Destroy(gameObject);
    7.     }
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    My guess would be that you have the model for either your enemy or for the new pickupitem not at 0,0,0 of its parent object, so when the pickupitem is instantiated at the position of the enemy it appears to be at a different position even though it is at the same position.

    Compare the position of the enemy that you use in transform.position when instantiating and compare that to the position of the pickupitem after instantiated. You could do that visually in the editor or by just adding a couple debug.log lines.

    Code (CSharp):
    1.  
    2. Debug.Log("Enemy: " + transform.position);
    3. PickupItem instance = Instantiate(pickupItem, transform.position,Quaternion.identity);
    4. Debug.Log("Drop: " + transform.position);
    5. instance.ItemDrop = item;
    6.  
     
    Last edited: Nov 2, 2017
  5. Avi3ator

    Avi3ator

    Joined:
    Feb 29, 2016
    Posts:
    64
    and also say the first instantiated object gets put like 2(meters?) down, the second goes 2.5 and the third goes 3(meter?) so for each item it goes lower and lower into the void