It dosent matter how I write the class but it always gives me one of thw errors:
—The class defined in the script file named ‘Cel’ is not derived from MonoBehaviour or ScriptableObject!
—You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
#pragma strict
class Cel extends MonoBehaviour
{
var h : float;
var g : int;
var f : float;
var obj : Transform;
var objPos : Vector3;
var color : Color;
var enemy : Transform;
var distance : Vector3;
var availible : boolean;
var inList : boolean;
var trueT : boolean;
var currentCellParent : Transform;
function Cel(gInt : int, object : Transform, enemyPos : Transform, parent : Transform)
{
g = gInt;
obj = object;
objPos = obj.transform.position;
currentCellParent = parent;
if(availible != true)
{
obj.renderer.material.color = Color.red;
}
}
}
If I use it without the "Extends MonoBehaviour" I get the first error......if I write it with the "Extends MonoBehaviour" I get the second error.
A weird thing is that I have severall other classes that dont use the "extends MonoBehaviour" and they work just fine.
#pragma strict
class MyClass
{
var number : int;
var blank : Color;
function MyClass(n : int, x : Color)
{
number = n;
blank = x;
}
}
Monobehavior is for scripts running on objects, to create your own class create it like you would any other C# class. Remember the using UnityEngine. And above the class name you have to declare it as serializable. Then, remember to SAVE it before you try to access it somewhere else, I had an issue with this. Consider this example class
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class Player{
public string name {get; set;}
public float score {get; set;}
public List<string> words {get; set;}
}
If you want a class that you attach to an object in unity and make it do something with that object, you make it extend monoBehaviour. You don’t need to create an object with “new” for this sort of class.
If you want to create a class to store some type of data, do not extend monoBehaviour. You cannot attach this type of class to an object directly, but you will be able to call new with your own custom constructor.
Now, if you want to make a class with your own constructor AND attach it to a game object, you’re going to have to make 2 classes. One is your class, the other will be a monoBehavior child that you will attach to your object and make it call new to your class.
As an example, I made my own class called gridCube that stores data for a single cube in a 3 dimensional grid. Then I made a monoBehaviour class gridData, that creates a whole grid out of gridCube objects. gridData is attached to a gameObject in unity.
gridCube.cs
public class gridCube
{
private bool myValue1;
public int myValue2;
public float myValue3;
public gridCube(bool newValue1, int newValue2, float newValue3)
{
myValue1 = newValue1;
myValue2 = newValue2;
myValue3 = newValue3;
}
gridData.cs
public class gridData : MonoBehaviour
{
public int gridSizeX;
public int gridSizeY;
public int gridSizeZ;
Dictionary<Vector3, gridCube> gridDictionary = new Dictionary<Vector3, gridCube>();
void Start ()
{
for(int i = 0; i <= gridSizeX; i++)
for(int j = 0; j <= gridSizeY; j++)
for(int k = 0; k <= gridSizeZ; k++)
gridDictionary[new Vector3((float)i, (float)j, (float)k)] = new gridCube(true, 0, 1f);
}
}
Note: My actual code makes sense and does something useful. I just removed all the goblegook to show as simplified an example as possible.