Properly got component but it is not reading the correct value from another script?

Hello, I have an issue. I have a variable saying if my player is alive and I have 2 other scripts accessing that variable, one works and the other doesn’t and I don’t understand why. I hope someone can help me. This is the script that won’t work, I have debug.logged the variable isAlive but it always says false even though it is true.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{

    public GameObject p;

    public Rigidbody2D rb2d;

    public float speed = 8f;

    public Player player;


    // Use this for initialization
    void Start()
    {
        player = p.GetComponent<Player>();
        
    }



    void FixedUpdate()
    {
        Move();
        
    }

    private void Move()
    {
        if (player.isAlive == true)
        {
            rb2d.transform.Translate(-transform.right * speed * Time.deltaTime);

            if (transform.position.x < -20f)
            {
                Destroy(this.gameObject);
            }

            if (Time.time > 50)
            {
                speed = 20f;
            }
        }



    }
}

This is the script I’m trying to access.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {

    public float jumpForce = 2f;

    private bool isGrounded;
    public bool isAlive = true;

    public Animator anim;
    
    public Rigidbody2D rb2d;
    


	// Use this for initialization
	void Start ()
    {
        isGrounded = false;
        isAlive = true;
	}

    
	void FixedUpdate ()
    {
		if(Input.GetMouseButton(0) && isGrounded == true && isAlive == true)
        {
            rb2d.AddForce(transform.up * jumpForce);
            isGrounded = false;
            anim.Play("Jumping");
        }
	}


    void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.transform.tag == "Ground")
        {
            isGrounded = true;
        }
        if(collision.transform.tag == "Enemy")
        {
            Destroy(this.gameObject);
            isAlive = false;
        }
    }

}

and this is the script I have that is working and accessing player correctly.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HUD : MonoBehaviour {

    public GameObject p;

    public float score;

    public Text scoreText;

    public Player player;
    

    // Use this for initialization
    void Start()
    {
        player = p.gameObject.GetComponent<Player>();
    }

    // Update is called once per frame
    void Update()
    {
        if (player.isAlive == true)
        {
            score = Time.time * 1.25f;
            scoreText.text = "Score: " + score.ToString("0");
        }
    }
}

Also, yes, I have put the game object to the appropriate spot in the hierarchy.

Good day.

Some tips:

At Player class, you are declaring the public variable isAlive, and setting it as true, and inmediatly in the Start() you set is true again. No need to do it twice, i recoomend to only make it true at the Start().

Aslo in Player, you have “Destroy(this.gameObject);” . The word “gameObject” (with g, not G) always is refeard to the gameObject containing the script, so no need to say “this.” . I mean this 2 sentences are exactly the same, always.

Destroy(this.gameObject);
Destroy(gameObject);

More tips, when checking a if sentence , like:

if (player.isAlive == true)

If you check a bool variable, as bool can only be true or false, yo ucan write it like this:
These 2 sentences are exactly the same:

    if (player.isAlive == true)
    if (player.isAlive)

and this 2 are also the same:

    if (player.isAlive == false)
    if (!player.isAlive)

And, more things, you are assigning the variable player via scripting when you say:

         player = p.gameObject.GetComponent<Player>();

So player variable no need to be public.

And now, lets talk about your problem.
Did you debugged the code? Try to put some Debug.Log to see if the executiuon is entering into the if sentences, like

if (player.isAlive == true)
         {
             Debug.Log ("Player is Alive");
             score = Time.time * 1.25f;
             scoreText.text = "Score: " + score.ToString("0");
         }

and

 void OnCollisionEnter2D(Collision2D collision)
 {
     Debug.Log("Collision Detected with "+collision.gameObject.name);
     if(collision.transform.tag == "Ground")
     {
         isGrounded = true;
     }
     if(collision.transform.tag == "Enemy")
     {
         Destroy(this.gameObject);
         isAlive = false;
     }
 }

This way you can easy track where the code start doing thing you dont want.

Once you discover where the problem is, come and give us feedback if you need!

Bye! :smiley: