Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 May 03, 2014 at 06:11 AM by Fattie for the following reason:

Duplicate Question

avatar image
5
Question by Moeen · May 02, 2014 at 11:22 PM · c#gameobjectgetcomponent

How to get a variable value from another script(C#)?

Hello, I understand the concept of getting a variable value from another script but only if there is a game object to use gameObject.GetComponent<"otherScript">(); but what if there is no game object?

This is the one that needs to be accessed:

   public class PointScript : MonoBehaviour {
     
         public int playerscore=0;
      
         //Start()
 
         public void OnTriggerEnter2D(Collider2D other){
             if (other.tag == "Player") {
                    playerscore = playerscore + 1;        
           }
         }
 
         //Update
     }

This is the one that needs to access my point script:

 public class HudScript : MonoBehaviour {
 
     private PointScript score= GetComponent<PointScript>();
     float PlayerScore = score.playerscore;

     //start()
 
     //update()
 }

The problem might be that I want to use the PointScript on multiple triggers.

Please reply and thanks for any help.

NB Please excuse any poor formatting, this is my first posted question.

Comment
Add comment · Show 2
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 Kamuiyshirou · May 03, 2014 at 12:31 AM 0
Share

For you to use a value of another variable should use a Statics class. However, still do not get the TYPE of value you want to get in another class. Can you explain your logic?

avatar image Fattie · May 03, 2014 at 06:11 AM 0
Share

You absolutely should not use a static in this case. the OP should just learn how to do it properly

1 Reply

  • Sort: 
avatar image
35

Answer by mattyman174 · May 02, 2014 at 11:36 PM

You want to use a Static variable in your PlayerScore class so that the variable becomes a member of the Class and not of any particular Instance of the Class you may have in your scene. You can then access that variable directly from the Class. See below.

 public class PointScript : MonoBehaviour {
 
     public static int playerScore;  //  Static keyword makes this variable a Member of the class, not of any particular instance.
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player")
             playerScore++;  //  The code that any instance can use to cause the score to be incremented, since the playerScore variable is a Static member, all instances of this class will have access to its value regardless of what instance next updates it.
     }
 }

Now to access it via an outside class, all you need to do is use the Class name. As such.

 public class HudScript : MonoBehaviour {
 
     private static int score;  //  A new Static variable to hold our score.

     void Update()
     {
         score = PointScript.playerScore;  //  Update our score continuously.
     }
 }

I hope this answers your question. Any problems just let me know.

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 mattyman174 · May 02, 2014 at 11:49 PM 0
Share

I just used the following Test cases to prove that it works.

 public class PointScript : $$anonymous$$onoBehaviour {
 
     public static int playerScore;  //  Static keyword makes this variable a $$anonymous$$ember of the class, not of any particular instance.
 
     void Update()
     {
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
             playerScore++;  //  Increment when i press the "W" $$anonymous$$ey.
     }
 }

 public class HudScript : $$anonymous$$onoBehaviour {
 
     private static int score;  //  A new Static variable to hold our score.
 
     void Update()
     {
         score = PointScript.playerScore;  //  Update our score continuously.
     }
 
     void OnGUI()
     {
         GUI.Label(new Rect(10, 10, 200, 20), "Score: " + score);  //  Display the score on a label.
     }
 }

The more GameObjects with the PointScript attached, the more it will increase by when you hit W.

So with 2 it will increase by 2's everytime since both instances of the PointScript class have the same logic to update the playerScore variable.

avatar image seevenup · Feb 11, 2016 at 03:18 PM 0
Share

@mattyman174 thx man, i was looking for this since 4 days had the same problem. static, damit, learned something new

avatar image varosi · Jul 14, 2017 at 03:04 PM 0
Share

I am seeing that on 2017 but this comment is really helpful , thanks man you save my day.

Follow this Question

Answers Answers and Comments

29 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

Related Questions

C# GetComponent Issue 2 Answers

Accessing a value in a script attached to another game object 3 Answers

insert script question 1 Answer

Camera to follow a target within a circle? 1 Answer

Assigning current color to a variable for fade out (C#) 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