Instantiate a component creates a new game object?
Hi,
If I instantiate a component, does it create the new component with a new game object or clone it in the same game object that the component cloned?
LineRenderer old = new GameObject().AddComponent<LineRenderer>();
LineRenderer line = Instantiate(old);
line has the same game object as old or his own game object? Thanks
Answer by streeetwalker · Oct 01, 2020 at 05:27 PM
Hi, yes,, you don't instantiate components in and of themselves - a component always needs a gameObject wrapper. What are you trying to do?
If you read the documentation - from 2019.4: "This function makes a copy of an object in a similar way to the Duplicate command in the editor. If you are cloning a GameObject you can specify its position and rotation (these default to the original GameObject's position and rotation otherwise). If you are cloning a Component the GameObject it is attached to is also cloned, again with an optional position and rotation. "
The main goal I wanted to achieve is described in this question
https://answers.unity.com/questions/1778024/why-my-pivot-point-is-only-taken-on-the-first-line.html
Can you help me, please?
Answer by roroquessada · Oct 04, 2020 at 10:11 AM
I just want to create another linerenderer by instantiate the old linerenderer and rotate the new around a random point of the old.
But, I noticed a shift between the two and I think it's because of this stuff. I hope :D Thank you.
$$anonymous$$ost components do not allow multiple copies on given gameObject. This is true of the LineRender component - a game object can have only one.
You can coordinate the points of two LineRenders by specifying the positions of the points, so I think it should be easy to do what you want.
If the LIneRender points are specified to be relative to the game object position it is a component of, then yes, you would also want to position and rotation of th gameObject, either directly in the instantiate statement or after It is instantiated.
If the LIneRenderer point position option is set to be relative the World, then the points on the line are not affected by the gameObject's transform.
Your answer
Follow this Question
Related Questions
Start coroutines when the class is not a MonoBehaviour 1 Answer
My components are disappears 1 Answer
New UI - Referencing deep nested components on an prefab instantiation 1 Answer
Why isn't this instantiation cast working? 1 Answer
Game Manager can't decide what the instance of the object is despite just making an instance of it 1 Answer