Tuesday, June 18, 2019

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


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.




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...