I create a new C# class , summarized in
using UnityEngine;
using System.Collections;
public class ExtremeTest : Component {
public float PosX,PosY,PosZ;
void Start(){
PosX=PosY=PosZ=0;
}
}
and I , in the camera script , initialize it , using
public ExtremeTest ET=null;
void Start(){
ET=new ExtremeTest();
if (ET==null){
print("error");
}
}
Well , the log give me “error” (then the ET is null)!
WHY???
Thanks for every help…
If that’s supposed to be an in-game object, try using Instantiate instead: Unity - Scripting API: Object.Instantiate
Do no instantiate components with new.
Components need to be attached to gameobjects to have any of their functionality, and the only way to attach a component onto a gameobject is to instantiate a new one with .AddComponent(). This means that if you instantiate it any other way you will never be able to have it function correctly or attach to a gameobject.
Therefore either use .AddComponent, or do not inherit from component.
Oh yeah, that makes sense. Serves me right for not thinking too hard about this one. 