Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by robbie696 · Jan 08, 2016 at 04:56 PM · inventory1st personaccessingitem pickup

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RPG Picking up items with polymorphism - probably quick :) 1 Answer

Adding a prefab to the Item class instance 0 Answers

Find the name of item being picked up - C# 1 Answer

My Inventory system is in a big mess now, help me, please 0 Answers

Inventory Master 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges