Need help with accessing a variable from another script
Hello,
I have been using unity for a while now but this is the first time i need to access a variable from another script and i do not fully understand how to do this. I have been looking for an answer but these don't work for me and i don't know what i'm doing wrong.
I currently have a wood object called "Wood3" and the object has a script attached to it called "PickUp". In the PickUp script i have a public int called woodAmount. But today i wanted to add a campfire wich needs wood before it ignites. So i got a campfire object and attached a script to it called "CampFireFuel" and i want to check if the player has wood to be able to ignite the campfire.
These are the script i am using:
====== PickUp script ======
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class PickUp : MonoBehaviour {
public GameObject PickupText;
public Text countText;
public int woodAmount = 0;
void OnTriggerStay(Collider collider)
{
if (collider.gameObject.name == "PlayerRange")
{
PickupText.SetActive(true);
}
if (collider.gameObject.name == "PlayerRange" && Input.GetButtonUp ("Use"))
{
Debug.Log ("You collected wood!");
woodAmount ++;
countText.text = "Wood: " + woodAmount.ToString ();
Destroy (gameObject);
PickupText.SetActive(false);
}
}
void OnTriggerExit (Collider collider)
{
PickupText.SetActive (false);
}
void Start ()
{
countText.text = "Wood: " + woodAmount.ToString ();
woodAmount = 0;
}
}
====== CampFireFuel script ======
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class CampFireFuel : MonoBehaviour {
public Text ActionText;
public GameObject CampfireLight;
public GameObject HeatParticles;
public GameObject FlameParticles;
public GameObject SparkParticles;
void Start () { GameObject.Find ("Wood3").GetComponent ();
ActionText.text = "";
CampfireLight.SetActive (false);
HeatParticles.SetActive (false);
FlameParticles.SetActive (false);
SparkParticles.SetActive (false);
}
void OnTriggerStay (Collider collider) { if (collider.gameObject.name == "PlayerRange") { ActionText.text = "Press E to ignite the fire (-1 wood)"; }
if (collider.gameObject.name == "PlayerRange" && Input.GetButtonUp ("Use"))
{
if (woodAmount == 0)
{
ActionText.text = "You do nit have enough wood!";
}
if (woodAmount >= 1)
{
woodAmount --;
CampfireLight.SetActive (true);
HeatParticles.SetActive (true);
FlameParticles.SetActive (true);
SparkParticles.SetActive (true);
ActionText.text = "";
}
}
}
void OnTriggerExit (Collider collider) { ActionText.text = ""; } }
Thanks for the help and if you need any more information just ask.
Answer by RedHedZed · Jan 08, 2016 at 05:19 PM
The formatting on your script there is a little weird, but it looks like in CampFireFuel's Start() you're finding Wood3 but not doing anything with it. You could do something like this in your CampFireFuel script:
PickUp woodPickup;//variable of type PickUp
void Start(){
//This will find your Wood3 object and assign its PickUp component to woodPickup
woodPickup = GameObject.Find("Wood3").GetComponent<PickUp>();
}
Then later in your CampFireFuel script you can access the woodAmount variable by saying:
woodPickup.woodAmount;
Your answer
![](https://koobas.hobune.stream/wayback/20220612055455im_/https://answers.unity.com/themes/thub/images/avi.jpg)