[2.6.1] C# Classes question

Hello,

I am having a problem, I created class, it does not inherit from MonoBehavior:

using UnityEngine;
using System.Collections;

public class ProjectSettings {

	
		//Scrolling speed
	private float _baseSpeed;
	
	public void Init(){
		
		
		_baseSpeed = 2.5f;

	}
	
	public float GetBaseSpeed{
			get {return _baseSpeed; } 
    }

}

In my main game class I declare and define an instance of this class

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GameMain : MonoBehaviour {

	public ProjectSettings PS;

	void Awake () {
	
		PS = GetComponent<ProjectSettings>();
		
		//PS = new ProjectSettings();
		PS.Init();

}
}

Everything works, but unity says:

“Assets/Scripts/GameMain.cs(60,20): error CS0117: ProjectSettings' does not contain a definition for Init’”

How come?

Thanks

If you aren’t inheriting from MonoBehaviour then why not just use the constructor?

public class ProjectSettings {
      private float _baseSpeed;
      public ProjectSettings() {
            _baseSpeed = 2.5f;
      }
}

And then somewhere else:

      public ProjectSettings ps = new ProjectSettings();

I don’t usually write in C# so someone please jump in here if I’m wrong…

ahh damn, you are right, totally forgot, thanks

Nope you are right Kelso :slight_smile:

To explain it:

GetComponent can only find components (that means instances of class Component, Behaviour and MonoBehaviour or extends of it. In unity, Component and MonoBehaviour are reflected through classes you can drag onto a game object), if you don’t inherit from MonoBehaviour, you will have to keep own references and manage them completely yourself, to unitys management, mainloop etc they “don’t exist” kind off.