Hello, I’ve overloaded some operators in a class and want to assign a component to a “new” one. Here is the code:
GetComponent<Stats>() = finalStats / (generation - 1);
The error I get is: The left-hand side of an assignment must be a variable, a property or an indexer.
Of course C# doesn’t let you overload the = operator (sigh). Am I allowed to assign a Component like this? If so, then the problem is probably in my overloaded operator.
edit: This doesn’t work either, so it seems the problem is not my operator.
Stats test = new Stats();
GetComponent<Stats>() = test;
Kiwasi
3
Use
Destroy(GetComponent<Stats>());
AddComponent<Stats>();
Components can’t exist without being attached to a GameObject.
You also can’t create a Component with the new keyword
You would use AddComponent to add a new component to a GameObject. GetComponent only returns an existing component. You can’t assign anything with GetComponent, and components are not created with the new operator.