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 /
This question was closed Jul 14, 2016 at 07:50 AM by x20X-Studios834 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by x20X-Studios834 · Jul 13, 2016 at 09:21 AM · 2dreferencing

Accessing a variable from one script in another.

Hey guys,I started coding a few months ago and I've been creating a simple game that features a JRPG style of combat. I need to keep track of all the different variables (action points, health, enemy health) and use them in other scripts, but I have no idea where to begin.

 using UnityEngine;
 using System.Collections;
 
 public class FightDraft1 : MonoBehaviour 
 {
     public float playerHealth = 100.0f;    
     public float enemyHealth = 100.0f;
     public float actionPoints = 10.0f;
     public float punchDamage = 5.0f;
     public bool turnEnd = false;
     public bool enemyDead = false;
     int raiderDamage;
     int raiderHit;
     int messagePlay;
     int playerHit;
     int playerDamage;
         
     void Start()
     {
 
     }
         
     void OnMouseDown()
     {
         playerHit = Random.Range (0, 11);
         playerDamage = Random.Range (5, 11);
         print (playerDamage);
         print (playerHit);
         if (actionPoints > 0.0f) 
         {
             if (playerHit >= 5)
             {
               enemyHealth = enemyHealth - playerDamage;
               actionPoints -= 10.0f;
               print ("You hit the raider.");
               print ("The raider took " + playerDamage + " points of damage.");
               print ("The raider has " + enemyHealth + " health points left.");
               print ("You have " + actionPoints + " Action Points left.");
             }
             else
             {
               print ("You missed the raider");
               actionPoints -= 10.0f;
             }
         } 
         else 
         {
             print ("You have no remaining action points");
             print ("Ending Turn...");
             turnEnd = true;
         }
     }
 
     void Update()
     {
         if (turnEnd == true) 
         {
 
             raiderDamage = Random.Range (3, 11);
             print (raiderDamage);
             raiderHit = Random.Range (0, 11);
             print (raiderHit);
 
             if (raiderHit > 5) 
             {
                 print ("The raider punches you");
                 turnEnd = false;
                 playerHealth = playerHealth - raiderDamage;
                 print ("The raider done " + raiderDamage + " damage.");
                 print ("You have " + playerHealth + " health remaining.");
                 actionPoints = 10.0f;
             }
             else
             {
                 print ("The raider missed");
                 turnEnd = false;
                 actionPoints = 10.0f;
             }
         }
         if (enemyHealth <= 0.0f)
         {
             enemyDead = true;
             print ("The raider has died");
             enemyHealth += 1.0f;
             Application.LoadLevel("Fallout'77-worldMap");
        }
         if (playerHealth <= 0.0f) 
         {
             print ("You Have Died.");
             Application.LoadLevel("deathScreen");
         }                
    }
 }

In the code attached, the player clicks the punch button, and it works out whether or not he hit, how much damage he dealt, how much health the enemy has remaining, and how many action points the player has left.

Since I didn't know how to reference the variables in other scripts, I also included the enemy turn in this script, though I would like to make it a seperate script.

I need to use these variables in other scripts in order to add other attacks and abilities.

Thank you for any help you can give.

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

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by DenisTribouillois · Jul 13, 2016 at 11:37 AM

You should look at this tutorial: https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data

It explains how to store data between scenes and between game executions, but it can be useful to understand the logic behind it: having an object controlling all the data you want to keep and reuse.

If you want a simpler solution, you can look at this thread: http://answers.unity3d.com/questions/42843/referencing-non-static-variables-from-another-scri.html

Comment
Add comment · Show 1 · 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
avatar image x20X-Studios834 · Jul 14, 2016 at 06:46 AM 0
Share

Thanks Denis, I was able to find a good solution on that thread, and I'll be sure to check out that tutorial.

avatar image
2

Answer by Tom01098 · Jul 13, 2016 at 11:06 AM

If you want to get a reference to your 'FightDraft1' script, you can write

 public FightDraft1 variableName;

in your script. This will create a reference box in the inspector, which you can drag the instance of the script you want into it. To access the playerHealth for example, you would use

 variableName.playerHealth

Good luck :D

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

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

2D Animation does not start 1 Answer

Have 2D Sprite Follow the Mouse in Straight Line 2 Answers

iOS ONLY stutter every 3 seconds simple 2d game,iOS ONLY constant stutter / lag every 3 seconds in simple 2D game 0 Answers

Unity 2D 4.6b UI: Why are instantiated prefab sprites rendering over UI elements? 1 Answer


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