I’m working on learning Unity & C#, so I’m just building a few random things in Unity to help me get the hang both Unity and C#, before I attempt to make an actual game. That being said I have a script that; seems like it should work, but doesn’t. Basically I’m attempting to change a variable in one script from a second script. PS. I’m very new to C# so please try and explain as simply as you can.
Script with my variables.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBasics : MonoBehaviour
{
public static int Life = 10;
public GameObject Player;
public static bool fullPower = false;
}
I’m attempting to change the Life variable.
From this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lose_Health : MonoBehaviour
{
void OnTriggerEnter(Collider loseHealth)
{
if (loseHealth.CompareTag ("Player")
PlayerBasics.Life -= 2;
Methods.HealthCheck();
}
}
I can’t understand why that doesn’t change the variable, because I have to problems doing the same thing to reference it in this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Methods : MonoBehaviour
{
public void fullPower()
{
//This is where the scripts for the main characters full power goes.
}
public void healthCheck()
{
if (PlayerBasics.Health <= 0)
{
//This should run after anything that deducts health
Death ();
}
}
public void Death()
{
//This calls the GameOver scene
}
}
I sincerely appreciate any help.