Hej,
I’m working on a problem and yes i searched a bit.
My player is able to receive different ‘conditions’. A condition is a class derived from the base class Condition.
So for example the condution Stone derives from Condition.
Here is the code to add the condition:
using UnityEngine;
using System.Collections;
public class AddCondition : PlayerTrigger {
// the condition we want to add
public Condition c;
// the players SpriteRenderer
private SpriteRenderer playerSpriteRenderer;
public override void Enter (GameObject _player, Rigidbody2D _playerRB)
{
// check if there are other conditions and remove them
Destroy(_player.GetComponent<Condition>());
// get the sprite renderer of the player
playerSpriteRenderer = _player.GetComponent<SpriteRenderer> ();
// play the change animation
// TODO
// change the sprite
playerSpriteRenderer.sprite = c.image;
// change the players mass
_playerRB.mass = c.mass;
// add the condition itself
_player.AddComponent<c>();
}
}
but it says error CS0118: 'AddCondition.c' is a 'field' but a 'type' was expected
i tried c.name, c.GetType() and typeof(c).
The trigger is a gameObject which has the above script attached. And the field c is obviously the derived class I want to add to the players gameObject and is set in the inspector.
i believe i saw some code that does this but i cant find it anymore
sidenote: i worked for many years as a Java and PHP / JS developer now so I habe to rethink a lot a my programing paradigms like this one. I think the compiler does now the type of component i want to add, its either condition or a derived class of it, And i dont know how to implement generics in this class since it derives itself from a class “PlayerTrigger” which checks if a collision is caused by the player and works as an interface with some overloaded methods and provides some variables for me, like the player object etc.
Thanks in advance