Skip to main content
Fixed the grammer, puncuation, and re-ordered to be more search friendly.
Source Link

This may be a common question , but I couldn't find any proper answer I wanted . Even-though some answers are in JavaScript , some are in Unity 4.x etc .

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

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 
{
     private 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 ; 
     }
}

nowNow 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  ?

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

This may be a common question , but I couldn't find any proper answer I wanted . Even-though some answers are in JavaScript , some are in Unity 4.x etc .

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

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 
{
     private 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  ?

Please help me , Thanks :)

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

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 
{
     private 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?

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.

Source Link
Nicolus Buck
  • 77
  • 1
  • 4
  • 11

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

This may be a common question , but I couldn't find any proper answer I wanted . Even-though some answers are in JavaScript , some are in Unity 4.x etc .

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

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 
{
     private 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 ?

Please help me , Thanks :)