How do I link variables in between two scripts in c#

guys I feel silly have been looking the whole day in unity answers but my script still doesn’t work been trying to use gameobject method to link my shieldHP with mytest manager to see if the shield would actually get destroyed and also sot htat I may do this in the future with other scripts
Here they are
First script:
using UnityEngine;
using System.Collections;

public class LightFolow : MonoBehaviour {

public Object Shield;
public Camera cam;
private float MaxW;
public Rigidbody2D Light;
Vector3 targetPosistion; 
public int LightHP;


// Use this for initialization
void Start () {
	if (cam == null) {
		cam = Camera.main;
	}
	Light = GetComponent<Rigidbody2D> ();
	Vector3 UpperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
	Vector3 targetWidth = cam.ScreenToWorldPoint (UpperCorner);
	MaxW = targetWidth.x;
}

// Update is called once per frame
void FixedUpdate () {
	Vector3 rawPosition = cam.ScreenToWorldPoint (Input.mousePosition);
	Vector3 targetPosition = new Vector3 (rawPosition.x,rawPosition .y,0.0f);
	float targetWidth = Mathf.Clamp (targetPosition.x, -MaxW, MaxW);
	targetPosition = new Vector3 (targetWidth, targetPosition.y, targetPosition.z);
	Light.MovePosition (targetPosition);

	if (LightHP == 0 || LightHP < 0)
		Destroy (Shield);
	

}

}
and then :
using UnityEngine;
using System.Collections;

public class TestManager : MonoBehaviour {

GameObject Shield;
 

// Use this for initialization
void Start () {
	LightFolow ShieldHealth = Shield.GetComponent<LightFolow> ();
}

// Update is called once per frame
void Update () {
	if (Input.GetKeyDown(KeyCode("K"))
	    ShieldHealth.LightHP - 20;
}

}

  • I would also like to do this for my shield and player since they bother have the same target position obviously so if someone could help me carry that value over would be great
    Player script:
    using UnityEngine;
    using System.Collections;

public class PlayerChontrol : MonoBehaviour {

public Camera cam;
private float MaxW;
public Rigidbody2D Player;
Vector3 targetPosistion; 


// Use this for initialization
void Start () {
	if (cam == null) {
		cam = Camera.main;
	}
	Player = GetComponent<Rigidbody2D> ();
	Vector3 UpperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
	Vector3 targetWidth = cam.ScreenToWorldPoint (UpperCorner);
	MaxW = targetWidth.x;
}

// Update is called once per frame
void FixedUpdate () {
	Vector3 rawPosition = cam.ScreenToWorldPoint (Input.mousePosition);
	Vector3 targetPosition = new Vector3 (rawPosition.x,rawPosition .y,0.0f);
	float targetWidth = Mathf.Clamp (targetPosition.x, -MaxW, MaxW);
	targetPosition = new Vector3 (targetWidth, targetPosition.y, targetPosition.z);
	Player.MovePosition (targetPosition);
	
	
}

}
Soz if the is bad I is nub and this is just a hobby but I would really like to learn more so pls help me :smiley:

I fail to see the significance of the “PlayerChontrol” class, is there a particular reason you posted it? In any case, I think your problem lies within the “TestManager” class.

You’re declaring the “ShieldHealth” as a local variable inside of the Start method. Then you attempt to use it inside Update. This isn’t going to work, if you want to have access to the variable throughout the class, you’ll need to declare it as a private variable inside of the class itself. Furthermore, if you intended to have the “LightHP” change, you’re going need to use a -= operator.

Updated

This might be more appropriate:

using UnityEngine; 
using System.Collections;

public class TestManager : MonoBehaviour
{
	 GameObject Shield;
	 private LightFolow ShieldHealth;

	// Use this for initialization
	 void Start () {
		 ShieldHealth = Shield.GetComponent<LightFolow> ();
	 }
	 
	 // Update is called once per frame
	 void Update () {
		 if (Input.GetKeyDown(KeyCode("K"))
			 ShieldHealth.LightHP -= 20;
	 }
}