Cannot create Components using the new keyword.
Adding created component to game object.
I have a created Collider similar to this:
MeshCollider col = new MeshCollider();
And I want to add it to a GameObject.
I can't use GameObject.AddComponent because it takes a type and creates one of that type, I need to add a created one not create one.
Please don't tell me I should do it differently, that's not my question (:
Is there a way to add a component that I already created?
EDIT
The conclusion was, "it can't be done". Assumedly due to the fact that Unity is coded in C++. Though you can create components with their C# type constructor ("new MeshCollider()") it's not actually useful because "Unity manages their own constructors".
So you have to create the component attached to a GameObject with AddComponent() to be useful.
So use:
gameObject.AddComponent<MeshCollider>()
What can't you add a mesh collider to the gameobject and capture a reference to it, then adjust it as needed? You'd have to have a pretty good reason for not doing so. Why are you creating the collider separately?
e.g.
$$anonymous$$eshCollider col = gameObject.AddComponent<$$anonymous$$eshCollider>();
Because I wanted to know if I could, it would be more convenient.
you can't. unity handles it's constructors all by itself (I guess it has to do with the unmanaged c++ engine core). If you want any component on any gameobject and a reference, use JoshuaHodkinsons example, which is not even that much longer code to be written
Please post your solution or this question will be closed for not being clear.
Um, the answer was, "you can't".
I'll add it though for the sake of others, who may have stumbled into the wrong question.
Yes, this is the correct answer. :)
I just didn't understand what "It's not longer but where, but I made it work." was supposed to mean.
Follow this Question
Related Questions
How to get a custom component declared by variable? 1 Answer
Unable to correctly use game objects? 2 Answers
Can not access GetComponentInParent ( ). usegravity 1 Answer
Error CS1519 1 Answer
Animation won't stop (Solved) 2 Answers