Question by
Seyed_Morteza_Kamaly · Dec 01, 2015 at 06:17 AM ·
unity 5
Global Variables for Scripts in untiy c#
i want have global int in unity that i can access to variable without getcomponent for example we have 2 source code:
A.cs:
using UnityEngine;
using System.Collections;
public class A : MonoBehaviour {
global int health; //suppose we have global int in unity.
void Update () {
}
}
B.cs:
using UnityEngine;
using System.Collections;
public class B : MonoBehaviour {
void Update () {
health--; // i want have global int that i can call it without getcomponent.
}
}
Output: health: 100 99 98 97 . . . -infinity
Comment
Answer by Seyed_Morteza_Kamaly · Nov 30, 2015 at 06:42 AM
finally i can solve my problem
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
//Static variables are shared across all instances
//of a class.
public static int playerCount = 0;
void Update()
{
Debug.Log (playerCount);
//Increment the static variable to know how many
//objects of this class have been created.
}
}
using UnityEngine;
using System.Collections;
using UnityEngine;
using System.Collections;
public class counter : MonoBehaviour {
// Use this for initialization
// Update is called once per frame
void Update () {
Player.playerCount++;
}
}
Your answer

Follow this Question
Related Questions
Baked lightmap in unity 5.2 not affect on android device 2 Answers
Map creator inside my game 0 Answers
Help with Pause Menu 1 Answer
Advice on creating 2d Inventory System with Unity 0 Answers
iron sight using inverse kinematic 0 Answers