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
if (range < 10)
{
Debug.Log("Nothing dropped");
}
//if (range == 10)
if (range == 10)
{
//code for loading the healthpack
Instantiate(Resources.Load("HealthPack"));
Debug.Log("Health kit dropped");
}
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…
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.
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:
private bool isPlaneDead;
// Determines if plane is dead or not
private Transform planeObj;
// This will store your plane objects Transform.position
void Update()
{
if(isPlaneDead == true)
{
// Let's assume your Plane is tagged "Plane"
// Use Find to locate your plane object "planeObj"
// Store the transform information
planeObj = GameObject.FindWithTag("Plane").transform;
// Make sure you run this check only one time
// Don't add this if your plane is for example falling from the sky
// You will want to constantly update the new coordinates.
// Note- Using find will not be the best way to approach this.
// Adding this will grab the coordinates once upon death.
isPlaneDead = false;
}
}
Now all you have to do is this:
public GameObject healthPack;
// You want to drag you prefab in this field within the inspector
// Also note - This code will need to be on the same script as the above code I wrote
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:
private GameObject healthPack = Resources.Load("HealthPack") as GameObject;
Instantiate(healthPack, planeObj.position, Quaternion.Identity);
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
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.