0

I have an object which when it is destroyed it will leave something behind.

//Spawn object at point GameObject bonusDrop = (GameObject)Instantiate(bonusItem,target.transform.position, Quaternion.identity); //Set the transform to the canvas (without this line the position is correct) bonusDrop.transform.SetParent(canvas.transform); //object is completely off 
  • I would like to set it to the canvas as it is intended as a UI element
  • The object ends up in the correct position if i dont set the parent (but if I don't set the parent the UI element won't work as intended).
  • I would love some help in trying to figure this out. I hate to say it but I feel the solution is simple and I can't get it =(

1 Answer 1

1

Check out: http://docs.unity3d.com/ScriptReference/Transform.SetParent.html

You are having an issue with world space vs local space. Your GameObject bonusDrop, before setting its parent, exists in global space meaning that its transform is based on the global origin of (0,0).

When you set the parent, your bonusDrop GameObject transform becomes local to the parent GameObject's transform.

This means that bonusDrop's transform is looking at the parents transform as the origin.

The function SetParent() has a second parameter which may help you out with this issue.

If you still have trouble, then let me know.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey thanks for your response! I first tried the False setting to keep the position. The only thing is once I do that, It seems to spawn the prefab in its original position now (i.e. where it in the game scene). I then set it to True and no luck. Is it perhaps cause my item drop is a UI Button by any chance? (Basicaly I want the droppable to be a button to interact with).
So by doing that, it spawns the prefab in the correct position? I am perhaps confused a little by what you are trying to do. Have you tried to perhaps create the button beforehand and leave it deactivated. Then at runtime when your object is destroyed have it activate the button and move it to the correct location?
Sorry I wasn't clear. Basically I'm trying spawn a UI element which acts as a pick up when an enemy dies. currently its a UI element button and when it spawns it goes into the bottom left corner of the screen everytime it is instantiated (spawned). Rather, I want it to spawn where the enemy died.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.