3

Details: In C# on Unity3d Searched on the unity documentation but the example doesn't fit to my case, same for other websites i visited. (i did not list them as i should.)

What i am trying to do is passing a class variable with a constructor to be a component:

CustomComponentClass ccc = new CustomComponentClass(2343, "blop");
gameObject.AddComponent(ccc);

I want to know if what i am trying shoud work, or if i missed something...

Problem:

gameObject.AddComponent(ccc); -> cannot convert ccc to string.

gameObject.AddComponent<ccc>(); -> Are you missing a using directive or an assembly reference? (i'm not, i'm using a variable and all is in the same namespace)

gameObject.AddComponent<CustomComponentClass>(ccc); -> UnityEngine.GameObject.AddComponent(string) cannot be used with the type arguments

gameObject.AddComponent<CustomComponentClass>(2343, "blop"); -> No overload for method AddComponent takes 2 arguments

Whatever i try doesn't work. C# is not really like javascript, if i'm right we used to be able to pass variables in the addcomponent in js.

Context: I must absolutely pass it in the constructor. I have 2 fields that are private, and i can't affort it tobe static, constants, or public for safety reason.

Options: If the answer is too complicated for me i will probably make get/set and check if the fields are empty.

user3071284
  • 6,955
  • 6
  • 43
  • 57
Po0ka
  • 107
  • 3
  • 9
  • You absolutely do not need to pass it in to the constructor. Instead create the component as normal (without parameters), add the component to the gameobject, then call a setup method on the component that provides the necessary parameters and runs whatever code you currently have in the constructor. – CodeSmile Jul 22 '14 at 16:30
  • Thought about making get/sets but never thought about a method to do the setup xD Thanks :P – Po0ka Jul 22 '14 at 16:35

1 Answers1

3

You absolutely do not need to pass it in to the constructor.

Instead create the component as normal (without parameters), add the component to the gameobject, then call a setup method on the component that provides the necessary parameters and runs whatever code you currently have in the constructor.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • I know this is old. But I'm also having this problem and don't quite understand this solution. What do you mean by *"call a setup method on the component that provides the necessary parameters and runs whatever code you currently have in the constructor"*? Is the setup method in the component? If so, how will it know what parameters to provide? Sorry if it's a newb question, been stuck at this for some hours now haha. – UndercoverCoder Aug 01 '20 at 21:23
  • Nevermind, figured it out. I first set the `gameObject.SetActive(false)` added the component `MyComponent myComponent = gameObject.AddComponent()` then changed what I needed to change `myComponent.SetMyVar("example")` then set the game object to active `gameObject.SetActive(true)`. Adding this in case anyone ran into the same confusion I ran into. – UndercoverCoder Aug 01 '20 at 21:49