- Home /
Calling a method of a class that is a part of another class?
Hello Devs,
Lets say I have a file called as LevelManager.cs and it looks like this
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class LevelManager : MonoBehaviour
{
// code
}
public class LevelInformation
{
static float LowestPoint, HighestPoint, HeightOfTheTree;
static int CurrentLevelNumber;
public static int GetCurrentLevelNumber ()
{
return CurrentLevelNumber;
}
}
Now outside of this file, can I call a method like this ->
Debug.Log(LevelInformation.GetCurrentLevelNumber ());
I am under that assumption that as
It does not inherit monobehaviour
It is a public class and a public method
The variable is a static variable
I can call this directly. Basically I am trying to call its method.
Please do correct if I am wrong.
You help is much appreciated.
Thank you,
Karsnen.
Dave, I thought the answer is solved? Why did you take it out?
Answer by jogo13 · Jan 31, 2013 at 07:18 PM
A static method cannot require a reference to another variable outside of itself. (Unless you grab the variable within the method, like using GameObject.Find("name").GetComponent)
I believe if you just make the class itself static, then you can access the level number like so: int number = LevelInformation.CurrentLevelNumber.
But you can only have one 'copy' of that class. (Singelton)
Making the class static:
public static class LevelInformation
You could then most likely remove the static keywords from within the class.