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 NicolusBuck · Jan 10, 2016 at 07:15 AM · c#unity 5variablesaccessingaccessing from any script

How to access a variable from another C# script in Unity

Hi Everyone ,

I am working on a simple game project, and have a problem. The problem is accessing the variable from one script to another.

I searched for, but couldn't find any proper answer I wanted. Even-though some answers are in JavaScript , some are in Unity 4.x etc.

For example :-

I have 2 Scenes (Game Scene and Game Over Scene), Game Scene has a Player Game Object , who has a int Variable Score.

 using UnityEngine;
 using System.Collections;
 
 public class Game_Scene_Script : MonoBehaviour 
 {
      public int Score ;
 
      // Use this for initialization
      void Start () 
      {
           Score = 0 ;
      }
 
      // Update is called once per frame
      void Update () 
      {
           // Let us assume some one played the game
           // And Now the Score is 207
           Score = 207 ; 
      }
 }


Now I want to access that int Variable Score in Game Over Scene which a Game Object.

So how can I access that int Variable from that script ?

Regards :-)

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 gjf · Jan 08, 2016 at 06:10 PM 0
Share

you need to find the game object containing the other script and then get a reference to it using GetComponent()

avatar image NicolusBuck gjf · Jan 09, 2016 at 04:17 AM 0
Share

Can you explain it ? thanks for reply

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LazyElephant · Jan 10, 2016 at 07:45 AM

You can find two ways to do it explained here: http://blog.christianhenschel.com/2013/05/16/how-to-pass-data-between-scenes-static-variables/

The second option he explains would probably be the easiest for your situation. By making the Score variable static, it wouldn't be reset on a new scene load, so as long as you have a Game_Scene_Script in your Game Over Scene, you could access the score.

This means you will have to remember to reset the score yourself. Also, in the event you need more than one of these scripts running, the Score variable will be shared between all instances of the script.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Varialbes on prefab script always null 0 Answers

Easy question: X axis overriding Y. Please help! :D 0 Answers

C# Unity dot syntax exercise correct solution? 1 Answer

Variable is not being constant through script execution 1 Answer

Calling Function from another script not working 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