How do you change a gameObjects speed when it loses a child?

I’m new to Unity and C# (Heck, I’m new to scripting), but can I ask some of you to help me with this? You don’t need to give me a code (although that would really help), I can suffice with an idea or hint.
Basically, I did 2 gameObjects: the first one is “Player” and the other one is “Shield” (“Player”'s child).
How do you change “Player”'s speed when “Shield” gets destroyed?
I’m currently making “Player” move with:

	public float playerSpeed;
	void Update () 
	{
		if (Input.GetKey (KeyCode.W))
			transform.Translate (Vector3.forward * playerSpeed * Time.deltaTime);
    }

So I want to know how to change “public float playerSpeed” when “Shield” goes away.
Thank you in advance. :slight_smile:

I would think whatever code is destroying the “shield” game object would have a reference to the parent Player object and just set the speed that way, but you could go about it as follows too: In your Shield script, add:

void OnDestroy()
{
    Transform parent = transform.parent;
    if (parent != null)
    {
        GameObject playerObj = parent.gameObject;

        // Note: I call it PlayerScript because you didn't say what the name of your player script class was
        PlayerScript player = parentObj.GetComponent<PlayerScript>();
        player.playerSpeed -= 10;
    }
}

EDIT: A couple of notes. You don’t necessarily have to get the gameObject from the Transform, because calling parent.GetComponent() should work the same way. I just wrote the code explicitly. Another even faster way, but potentially deadly, is to just call GetComponentInParent(), but if for whatever reason you had another player script higher up than just the next parent level, it could choose that one instead, but unlikely…and in this case wouldn’t make sense to have another player script anyway. The other positive to doing it the way the code is written above instead is that GetComponentInParent (and GetComponentInChildren) are recursive while just accessing the parent component directly is a single hop, if you will, to get to where you want to be.