0

I have a character that I can drag and drop around the canvas.

If you drag it onto a square, it dissappears.

All of that works well, however I want to Instansiate a clone of that character in the middle of the canvas again once it has been destroyed.

My code to do that so far is;

var clone = Instantiate(gameObject, startPosition, Quaternion.identity);
iTween.ScaleTo(gameObject, new Vector3(0, 0, 0), 2f);
Destroy(gameObject, 3f);

However the object is being cloned I think but I can't see it, and I also have no idea where it is.

Any advice?

Chud37
  • 4,907
  • 13
  • 64
  • 116
  • you should be able to find it in the scene view and then be able to work out why you cant see it – BugFinder Feb 24 '20 at 12:15
  • You are scaling to 0, isn't it? If you scale something to 0, is so little that you can't see it! – Lotan Feb 24 '20 at 12:19
  • @Lotan but I'm not scaling the clone – Chud37 Feb 24 '20 at 12:25
  • @BugFinder Thanks, that helps a lot. So it turns out its not Instantiating inside the canvas, which I fixed, but now its absolutely huge. Do you have any idea why that is? The original character is 400x400, when it gets cloned its 1000x1000 – Chud37 Feb 24 '20 at 12:26
  • try using clone.SetParent(canvasTransform, false); – Everts Feb 24 '20 at 12:27
  • Also, you need to look into the anchoring and size delta of your new object. – Everts Feb 24 '20 at 12:32

1 Answers1

2

UI objects have to be somewhere nested under a Canvas .. if you use Instantiate without passing any parent the GameObject is instantiated on root level without any parent => Your UI stays invisible.

Either pass it already to Instantiate

var clone = Instantiate(gameObject, startPosition, Quaternion.identity, parentWithinCanvas.transform);

directly set transform.parent

clone.transform.parent = parentWithinCanvas.transform;

or use transform.SetParent

clone.transform.SetParent(parentWithinCanvas.transform, false);

where the last parameter decides whether the object should keeps its current position in the world or not.

derHugo
  • 83,094
  • 9
  • 75
  • 115