Change a scale of a instance in C#

Hi everyone!

I’m trying to create an instance with the same scale as the GameObject that stores it, here is the code:

The problem here is that the instantiated GmObj. seems like it changes its scale to original’s prefab whatever the parent scale is, something like this:

How can I fix this? I don’t understand the localScale very well.

Thank you :slight_smile:

If you want a child to have the same scale as the parent, the child should have a scale of (1,1,1).

When you parent something, it tries to adjust the scale so that it remains visually the same size. So if the parent was already at scale=0.5, and you add a child object of scale 1, it would get a scale of 1.5 to compensate.

The function Transform.SetParent() has an optional boolean to determine whether or not the world-space visual should be preserved after parenting.

1 Like

Ok then, I’m trying to make the SetParent like this:

GameObject go = Instantiate (objMngr.prefab [iiii]); 
go.gameObject.transform.SetParent (gObjMngr);

But console now its telling that “cannot convert UnityEngine.GameObject' expression to type UnityEngine.Transform” :confused:

Yes, SetParent takes a Transform, not a GameObject. So it would be gObjMngr.transform.

1 Like

Finaly it worked, it seems like this (for someone who can use it in the future):

GameObject go = Instantiate (objMngr.prefab [iiii]);
go.transform.SetParent (gObjMngr.transform, false);

Thank you!! :smile:

1 Like

You should actually be able to combine that all into one line:

GameObject go = Instantiate<GameObject>(objMngr.prefab [iiii], gObjMngr.transform, false);

Then I get this

“Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject’. An explicit conversion exists (are you missing a cast?)” :confused:

Oh I forgot when you add parameters it returns an Object instead of a GameObject, see my edited post for the solution. You can cast to a GameObject or use the generic version of Instantiate where you specify the type in the angle brackets <>.

This is some standard programming stuff though, you should look into more C# oriented tutorials to get more familiar with Types and the syntax of the language. If you googled those errors you could’ve found examples of how to fix it, the error plainly states what’s wrong.

1 Like

I was trying to solve this quite a lot xD but I’m not so good at finding as I expected. And that’s what I’m doing, I’m trying to learn as much as I can and that’s why I came here ^^

Very thanks for the help :smile: xD

2 Likes