Search Unity

Strange script behaviour?

Discussion in 'Scripting' started by JohnGalt, Dec 4, 2007.

  1. JohnGalt

    JohnGalt

    Joined:
    Nov 27, 2007
    Posts:
    85
    I have one script called "MyTest". Then, I want to use it but without adding it to any gameobject, just by making a "new" of the script class. Here is the other script "OtherScript.js" :

    private var mTest : MyTest;

    function Start()
    {
    mTest = new MyTest();
    }

    function Update ()
    {
    Debug.Log(mTest); /* LOGS "MyTest" */
    if (mTest != null)
    {
    /* NEVER ENTERS HERE !!! */
    Debug.Log("Hello");
    }
    }

    It seems like in the Update function the "mTest" instance is set to something (If I log it, I get "MyTest" instead of null) but later the comparation is null...

    What am I missing here??

    Thank u!
     
  2. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    Does the MyTest have a default constructor?
     
  3. Talzor

    Talzor

    Joined:
    May 30, 2006
    Posts:
    197
    If MyTest inherits from MonoBehaviour (and since you use js it does by default unless you specify otherwise) the equality operators (and method) are overloaded to return null if the script isn't attached to a gameobject.

    In case UT passes this way: This is extremly annoying!
     
  4. JohnGalt

    JohnGalt

    Joined:
    Nov 27, 2007
    Posts:
    85
    grrrrrrrrrrr

    grrrrrrrrrrrrrrrrrrrrrr


    Thank you!!! I have resorted to have it attached to the gameobject... but I'll try to overload it again so to override this behaviour
     
  5. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108