[Solved] Inheritance + Corotuines + transform.position

I have a class Character. In this class I use a Coroutine to move the gameobject in the game

public class Character : MonoBehaviour
{
    protected IEnumerator TranslateCorout(Vector3 destination, float speed)
    {
        Vector3 start = t.position;
        Vector3 end = destination;
        float interpoler = 0f;

        while (interpoler <= 1f)
        {
            interpoler += speed;
            t.position = Vector3.Lerp(start, end, interpoler);

            yield return new WaitForSeconds(WaitTime);
        }

    }
}

I have then another class, which inherits from Character, with a function that uses the parent coroutine.

public class Hero : Character {
    public void Walk()
    {
            anim.SetTrigger(WALKANIMATION);
            Vector3 destination = new Vector3(t.position.x + 1f, t.position.y, t.position.z);        
            StartCoroutine(TranslateCorout(destination, movementspeed));
    }
}

Aaaaand, it doesn’t work. He triggers the animation, but it does not move.
The TranslateCorout was in the hero class, before, and everything was fine. Now, putting in order the code, I moved it to the parent class (to avoid repetition of the code in other subclasses which want to translate).

The funny thing is: I cannot modify at all the trasform.position value in the parent class.
I can read it, I can -apparently- set it, but if I print it again, after modifying it, it’s not changed. And thus, the movement issue.

Why a corotuine can’t change the transform.position field, and the subclasses inherits correctly?
What am I doing wrong?
Thanks for the answers ^^

When I saw your post, I was curious. I wasn’t aware of this. I’m going to keep a watch on this thread to see if there’s something I can learn :slight_smile:

Hm, it works for me. I mean I have only a simply ‘dummy’ setup with a base class (holding the Enumerator) and a child class inheriting & calling it.

Should work. Try putting Debug.Log’s in the coroutine, it’ll fire. There’s nothing special about inheritance here. Maybe movementSpeed is 0?

yep. The issue is not on the inheritance.
Neither on Coroutines.

It’s on the transform. position. It’s the only spot in the code where I am modifying it’s value. And if I print the value, I can see that it’s correct. But it doesn’t move. On the inspector its value never changed.

Is it a known bug? Is there a way to lock a gameobject in a specific position and I did it accidentally?

I mean…

This screen shows how I tried to move the hero with the translate coroutine as above (I’m printing the transform.position with a Debug.Log) from its initial position [-1,-1,0] to [0,-1,0]

In the log, everything seems ok. But the transform hasn’t changed.

So, what am I changing? Why is not modificable? (I was moving that gameobject in this exact way until now…)

The only explanation is that your Mecanim animation is overriding the position of the character. Have a look at those and see if you can find anything.

Damn. This. So much this.

It was on an animation that was never called, also. Totally resolved. Thanks a lot, to everyone ^^

:slight_smile: Glad ya got it fixed.