Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by DubstepDragon · Jul 23, 2013 at 09:51 AM · c#accessing from any scriptanother scriptcalculations

Trouble with calling a variable from another script

I have always had trouble with doing this - calling variables from another script.

I have a c# script that calculates gold, and a c# script for enemy AI, including a health variable, etc... What I want, is when the enemy's HP goes down to 0 or below, a certain amount of gold is given to the player. That means, accessing the gold script and adding an amount to the total gold.

How can I do this? Thanks in advance!

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by clunk47 · Jul 23, 2013 at 04:59 PM

You don't need to have a static variable to do this. For example: Let's say you have a gameObject named "Player1", and another one named "Enemy1". Enemy1 has a script called "EnemyHealth". To access this, you first access the gameObject, then the script on the object, then the variable "hp" from that EnemyHealth script. This would work like so:

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour
 {
     GameObject Enemy1;
     int enemyHP;
     int increment = 100;
     
     
     void Update()
     {
         if(GameObject.Find("Enemy1"))
         {
             Enemy1 = GameObject.Find("Enemy1");
             enemyHP = Enemy1.GetComponent<EnemyHealth>().hp;
             if(enemyHP <= 0)
             {
                 GetComponent<GoldClass>().gold += increment;
                 Destroy(Enemy1);
             }
         }
     }
 }

This isn't specific because it is just an example. This simply focuses on how to access a variable from a script on another gameObject without needing to use static (single instance) variables. Click here for more information on GetComponent

Comment
Add comment · Show 2 · 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 DubstepDragon · Jul 25, 2013 at 09:29 AM 2
Share

Sweet! I never knew that could be done. Thanks again, $$anonymous$$r. Clunk47!

avatar image clunk47 · Jul 25, 2013 at 08:32 PM 1
Share

Always happy to help :D

avatar image
2

Answer by sona.viswam · Jul 23, 2013 at 10:02 AM

 int other = gameObject.GetComponent(ScriptName).newNo;

Another method : Make into

 public static int newNo;

Get variable as

 int otherNo = ScriptName.newNo;



Another method is that you can make into playerprefs

 PlayerPrefs.SetInt("PlayerScore", 10);

And get as

 PlayerPrefs.GetInt("PlayerScore");
Comment
Add comment · Show 3 · 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 DubstepDragon · Jul 23, 2013 at 01:41 PM 1
Share

you used "gameObject.GetComponent". But I am accessing an int, not a gameObject. But does it really matter?

avatar image clunk47 · Jul 23, 2013 at 04:51 PM 1
Share

You only want to use static if there is only ever ONE INSTANCE of the variable. Just to clarify.

avatar image Eric5h5 · Jul 23, 2013 at 05:19 PM 4
Share

Should be GetComponent(), no quotes.

avatar image
1

Answer by chillersanim · Jul 23, 2013 at 10:06 AM

You have multiple possibilities: You can add a public method to the gold script:

 public void AddGold (int amount)
 {
     this.gold += amount;
 }

That allows a controlled gold management, but isn't the most simple way.

You can also make the variable "gold" public so that the script can directly access the variable. But that isn't a very clean way (but it works).

To access the method or public field, you can use:

 // Get the script that manages the gold of the player
 var gScript = (GoldScript)player.GetComponent("GoldScript");
 // If you choose the way with the public field, use this line to add gold
 gScript.Gold += 50;
 // Else use this line of code
 gScript.AddGold(50);

"GoldScript" is the script that manages the gold
"player" is the gameobject that contains the script

Hope I was able to help.

Greetings
Chillersanim

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
avatar image
1

Answer by coAdjoint · Jul 23, 2013 at 02:01 PM

Although the answers above are acceptable, if you know that there is only one GoldScript in the scene you could use a singleton pattern and call

 GoldScript.instance.Gold += 50 

or whatever mechanism you use.

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

20 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

Related Questions

Distribute terrain in zones 3 Answers

Script can't find Component within Start() - C# 1 Answer

Multiple Cars not working 1 Answer

A node in a childnode? 1 Answer

Decrease life from another script 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