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

Moving Prefab, after Instantiate(Resource.Load)

Discussion in '2D' started by Wuggie88, Dec 4, 2019.

  1. Wuggie88

    Wuggie88

    Joined:
    Sep 17, 2019
    Posts:
    9
    Hello, so I'm working on a small project, where if you shoot a plane, there's a 1/10 chance, a health pack will drop, that part works.

    This is under the script, where the plane destroys itself when all conditions are met (so it's attached to the enemy plane where the health pack should drop from)

    however, I tried with putting in a vector 2 (it's a 2D game), i have also tried to "move" it after it spawned (this didn't work either, might have been some errors in it and i just deleted the code again)

    however, this is what works, where it spawns it at 0,0,0

    Code (CSharp):
    1.         if (range < 10)
    2.         {
    3.             Debug.Log("Nothing dropped");
    4.         }
    5.         //if (range == 10)
    6.         if (range == 10)
    7.         {
    8.             //code for loading the healthpack
    9.             Instantiate(Resources.Load("HealthPack"));
    10.  
    11.             Debug.Log("Health kit dropped");
    12.         }
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @Wuggie88

    Sorry, I did read your question 2 times, but what is the question?

    Do you have trouble placing the object to other location than 0,0,0 ?

    You should capture the created / instantiated object to a variable, then place the object. See the docs:
    https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

    In the examples, they might not modify position, but the principle is the same.
     
  3. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Why not put your gameObjects.transform.position inside of the instantiation call?

    Code (CSharp):
    1. Instantiate(Resources.Load("HealthPack"), transform.position, Quaternion.Identity);
    But it's really hard for me to know how your information is being passed through Resources.Load. If that contains your GameObjects transform information, then maybe you're Destroy() the enemy before it obtains the correct position? You can delay your Destroy(gameObject, 1f) to buy a second to instantiate. And access your GameObjects SpriteRenderer and set its active to false. You might as well do the same thing for the collider also. But the GameObject will disappear and the item should drop at current position...Maybe...
     
  4. Wuggie88

    Wuggie88

    Joined:
    Sep 17, 2019
    Posts:
    9
    Eses, I'm sorry if I was unclear, yes I need it to drop from wherever the enemy plane is when it dies. whenever I put anything after the "HealthPack" the whole script just stops working correctly, and the planes no longer delete themselves, and the HealthPack no longer drops anywhere.

    MisterSkitz hmm I did do something like this before but forgot the Quaternion (I'm very new to coding), so that might actually fix it! Think it's one of those times where you just get so frustrated at something and you forget the simple things :)

    Thank you both very much, I will try this when I'm at my pc again.

    and sorry if I did not express myself in the right way in the question again.
     
  5. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    All you really need to do is make a public GameObject planeObj;
    If the script isn't attached to the plane, then use something like:

    Code (CSharp):
    1. private bool isPlaneDead;
    2. // Determines if plane is dead or not
    3.  
    4. private Transform planeObj;
    5. // This will store your plane  objects Transform.position
    6.  
    7. void Update()
    8. {
    9. if(isPlaneDead == true)
    10. {
    11.  
    12. // Let's assume your Plane is tagged "Plane"
    13. // Use Find to locate your plane object "planeObj"
    14. // Store the transform information
    15.  
    16. planeObj = GameObject.FindWithTag("Plane").transform;
    17.  
    18.  
    19. // Make sure you run this check only one time
    20. // Don't add this if your plane is for example falling from the sky
    21. // You will want to constantly update the new coordinates.
    22. // Note- Using find will not be the best way to approach this.
    23. // Adding this will grab the coordinates once upon death.
    24.  
    25. isPlaneDead =  false;
    26. }
    27. }
    Now all you have to do is this:

    Code (CSharp):
    1. public GameObject healthPack;
    2.  
    3. // You want to drag you prefab in this field within the inspector
    4. // Also note - This code will need to be on the same script as the above code I wrote
    5.  
    6.  
    7.  
    8. Instantiate(healthPack, planeObj.position, Quaternion.Identity);
    Just to inform you a little about instantiation. The first field is a game object, the second field is a Transform, and the third field handles rotation. Quaternion. identity is basically saying "no rotation, instantiate as created naturally". However, I don't think you have to include rotation, so this wouldn't disrupt your code.

    I've never seen an instantiation attempt in the format you are trying to do. This is new to me but I find it interesting and unique. If you wish to stay with your technique then let me advise this:

    Code (CSharp):
    1. private GameObject healthPack = Resources.Load("HealthPack") as GameObject;
    2.  
    3. Instantiate(healthPack, planeObj.position, Quaternion.Identity);
     
    Last edited: Dec 5, 2019
    Wuggie88 likes this.
  6. Wuggie88

    Wuggie88

    Joined:
    Sep 17, 2019
    Posts:
    9
    I have made it work this morning (well for me it is at least) by adding the "transform.position, Quaternion.Identity" part after the healthpack, that actually worked perfectly for some reason, (as i thought it would, but i gave up a bit when the transform.position didn't work)

    the reason i did it like this, is that it seemed the most simple to just load it from the resource folder (i might be wrong in this)

    again, thank you very much for the help, and that's a good read up there! will be sure to have it handy when i need to instantiate something next time and i run into trouble
     
    MisterSkitz likes this.
  7. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    The confusion on your end about the transform.position part was kinda my fault because I failed to tell you that you don't need to attach a game object to it if the script is directly attached to that object. The script will reference it automatically. However, if it isn't attached, then you do need a reference to that object, hence, planeObj.transform.position.

    Never give up on something, just keep working on it until you figure it out. Even if you move onto something else and come back to it later. ;)

    Edit:
    Also note that referencing from the Resource folder simply makes the object not appear in the hierarchy (clone). I hear there is some advantages to using this, possibly saving memory, but if I'm not mistaking, it takes 10 clones to equal around 1MB, so using the Resource folder can save 9-10th of memory per 10 objects spawned. However, since most get destroyed quickly, in most instances, the save is very little. I'd like to hear someone speak about this that's more knowledgeable on the subject but that's what I go out of it in my research.

    Good Luck amigo! :)