Statics

Vérifié avec version: 4.1

-

Difficulté: Intermédiaire

How to create static variables, methods, and classes.

Statics

Intermédiaire Scripting

Enemy

Code snippet

using UnityEngine;
using System.Collections;
public class Enemy
{
//Static variables are shared across all instances
//of a class.
public static int enemyCount = 0;
public Enemy()
{
//Increment the static variable to know how many
//objects of this class have been created.
enemyCount++;
}
}

#pragma strict
public class Enemy
{
//Static variables are shared across all instances
//of a class.
public static var enemyCount : int = 0;
function Enemy()
{
//Increment the static variable to know how many
//objects of this class have been created.
enemyCount++;
}
}

import UnityEngine
import System.Collections
public class Enemy:
//Static variables are shared across all instances
//of a class.
public static enemyCount = 0
public def constructor():
//Increment the static variable to know how many
//objects of this class have been created.
enemyCount += 1

Player

Code snippet

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 Start()
{
//Increment the static variable to know how many
//objects of this class have been created.
playerCount++;
}
}

#pragma strict
public class Player extends MonoBehaviour
{
//Static variables are shared across all instances
//of a class.
public static var playerCount : int = 0;
function Start()
{
//Increment the static variable to know how many
//objects of this class have been created.
playerCount++;
}
}

import UnityEngine
import System.Collections
public class Player(MonoBehaviour):
//Static variables are shared across all instances
//of a class.
public static playerCount = 0
private def Start():
//Increment the static variable to know how many
//objects of this class have been created.
playerCount += 1