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 :D
As long as Shield contains a valid GameObject and the script exists on that object, the GetComponent should work.
This is very common question. You should refer to existing answers as the information is the same throughout.
Note that if you declare a variable (LightFolow ShieldHealth) in Start() it will only exists within Start and cannot be used outside. Declare it in the class as you have done in the other scripts.
Answer by Mako-Infused · Nov 20, 2015 at 02:23 PM
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;
}
}
Ty dude told you I'm a nub xD But it give me a parser error on ShieldHealth says it an unexpected symbol??
Oh, did you just copy/paste and replace what you originally had in the Test$$anonymous$$anager class? I didn't rewrite the script fully, you still have to include some of the stuff you had in there originally. I'll edit it my post to make the answer more complete.
Your answer
Follow this Question
Related Questions
Vector3 variable not changing 0 Answers
Health variable not changing 1 Answer
Problem with Vector2/Vector3.Angle? 0 Answers
Problems with vector3 in world / local space 1 Answer
Assign GameObject OnMouseUp not through inspector -2D 1 Answer