Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Javascript Enum == Error

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Mar 9, 2015.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I am trying to set up an enum for presets in a script.
    The code below is an example of what we are doing.
    Code (JavaScript):
    1. var DoorPresets : preset = preset.none;
    2.  
    3. function Update
    4. {
    5. if (DoorPresets == preset.Default)
    6. {
    7. Debug.Log("Example");
    8. }
    9. }
    Doing this, I receive an error telling me I cannot use the operator == on the left side of preset and the right side of System.Object.
    I cannot find much information online about these kinds of errors, or what causes them, I am hoping someone here can assist with this.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Where are you defining the preset enum?
     
  3. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Nowhere, I have another script that uses it in this way that does not define the enum either, but works, I am using that as a base of reference, how does the other script work if it does not define them?

    This is the script I am using as a reference:
    Code (JavaScript):
    1. var crosshairPresets : preset = preset.none;
    2.  
    3.  
    4. //Variables
    5.  
    6. var showCrosshair : boolean = true;
    7. var verticalTexture : Texture;
    8. var horizontalTexture : Texture;
    9. //Size of boxes
    10.  
    11. var cLength : float = 10;
    12. var cWidth : float = 3;
    13. //Spreed setup
    14.  
    15. var minSpread : float = 30.0;
    16. var maxSpread : float = 100.0;
    17. var spreadPerSecond : float = 300.0;
    18. //Rotation
    19.  
    20. var rotAngle : float = 0.0;
    21. var rotSpeed : float = 0.0;
    22. private var temp : Texture;
    23. private var spread : float;
    24. function Update(){
    25.  
    26.         //Used just for test (weapon script should change spread).
    27.         if(Input.GetMouseButton(1)) spread -= spreadPerSecond * Time.deltaTime;
    28.         else spread += spreadPerSecond * 2 * Time.deltaTime;  
    29.      
    30.         //Rotation
    31.         rotAngle += rotSpeed * Time.deltaTime;
    32.        
    33. }
    34. function OnGUI(){
    35.  
    36.         if(showCrosshair && verticalTexture && horizontalTexture)
    37.         {
    38.                 var verticalT = GUIStyle();
    39.                 var horizontalT = GUIStyle();
    40.                 verticalT.normal.background = verticalTexture;
    41.                 horizontalT.normal.background = horizontalTexture;
    42.                 spread = Mathf.Clamp(spread, minSpread, maxSpread);
    43.                 var pivot : Vector2 = Vector2(Screen.width/2, Screen.height/2);
    44.              
    45.                 if(crosshairPresets == preset.crysisPreset){
    46.                      
    47.                                                                                            //The 2 is the thickness, and the 14 is the length
    48.                         GUI.Box(Rect((Screen.width - 2)/2, (Screen.height - spread)/2 - 14, 2, 14), temp, horizontalT); //Top
    49.                         GUIUtility.RotateAroundPivot(45,pivot);
    50.                         GUI.Box(Rect((Screen.width + spread)/2, (Screen.height - 2)/2, 14, 2), temp, verticalT); //Right
    51.                         GUIUtility.RotateAroundPivot(0,pivot);
    52.                         GUI.Box(Rect((Screen.width - 2)/2, (Screen.height + spread)/2, 2, 14), temp, horizontalT); //Left
    53.                 }
    54.                
    55.              
    56.                 if(crosshairPresets == preset.shotgunPreset){
    57.              
    58.                         GUIUtility.RotateAroundPivot(45,pivot);
    59.                      
    60.                      
    61.                         //Horizontal
    62.                        
    63.                         GUI.Box(Rect((Screen.width - 14)/2, (Screen.height - spread)/2 - 3, 14, 3), temp, horizontalT);
    64.                         GUI.Box(Rect((Screen.width - 14)/2, (Screen.height + spread)/2, 14, 3), temp, horizontalT);
    65.                        
    66.                        
    67.                         //Vertical
    68.                        
    69.                         GUI.Box(Rect((Screen.width - spread)/2 - 3, (Screen.height - 14)/2, 3, 14), temp, verticalT);
    70.                         GUI.Box(Rect((Screen.width + spread)/2, (Screen.height - 14)/2, 3, 14), temp, verticalT);
    71.                 }
    72.                
    73.                 if(crosshairPresets == preset.triPreset){
    74.              
    75.                                                                 //The 2 is the thickness, and the 14 is the length
    76.                         GUI.Box(Rect((Screen.width - 2)/2, (Screen.height - spread)/2 - 14, 2, 14), temp, horizontalT); //Top
    77.                         GUIUtility.RotateAroundPivot(30,pivot);
    78.                         GUI.Box(Rect((Screen.width + spread)/2, (Screen.height - 2)/2, 14, 2), temp, verticalT); //Right
    79.                         GUIUtility.RotateAroundPivot(30,pivot);
    80.                         GUI.Box(Rect((Screen.width - 2)/2, (Screen.height + spread)/2, 2, 14), temp, horizontalT); //Left
    81.                         GUI.Box(Rect((Screen.width / 2), (Screen.height /2), 4, 4), temp, horizontalT);
    82.                 }
    83.              
    84.                 if(crosshairPresets == preset.none){
    85.              
    86.                         GUIUtility.RotateAroundPivot(rotAngle%360,pivot);
    87.                      
    88.                      
    89.                         //Horizontal
    90.                        
    91.                         GUI.Box(Rect((Screen.width - cWidth)/2, (Screen.height - spread)/2 - cLength, cWidth, cLength), temp, horizontalT);
    92.                         GUI.Box(Rect((Screen.width - cWidth)/2, (Screen.height + spread)/2, cWidth, cLength), temp, horizontalT);
    93.                        
    94.                        
    95.                         //Vertical
    96.                        
    97.                         GUI.Box(Rect((Screen.width - spread)/2 - cLength, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
    98.                         GUI.Box(Rect((Screen.width + spread)/2, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
    99.                 }
    100.         }
    101. }
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Quite frankly, I have no idea how that works at all then.
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    If that script is from another project, it can be completely valid. However, preset has to be defined somewhere, otherwise it can't work.
     
  6. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    After changing the top row of variables to:
    Code (JavaScript):
    1. enum doorpreset { Default, OnMouseLook }
    2. var DoorPresets : doorpreset = preset.Default;
    I am no-longer receiving the error, I am still unsure of how the other script works though.
     
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    This script snippet doesn't make sense, because you assign the DoorPresets a value from preset. Those are two different enums.
     
  8. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I am setting doorpreset, which is an enum type from the variable DoorPresets, it does work.
    But now I am having a different problem, I am referencing a script on another object, trying to detect OnMouseOver and trying to grab it from that object as a boolean and using the variable:
    var Door : MouseDetect;
    to find it, then using:
    Door = gameObject.GetComponent(MouseDetect);
    In the start function.
    From there I try to use the boolean called MouseOn, which detects the OnMouseOver, and using it in an if statement along with some other internal variables from the script.
    As soon as I press play, the Door field with the type MouseDetect clears for no reason, and all I get is:
    Object reference not set to an instance of an object
    Either the scripting language is incredibly vague, or Unity really need to work on their debugger, because I can't see any reason why it would lose the variable instance, I have used custom variable types many times without problems.
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It's certainly not Unity's fault :) gameObject.GetComponent searches on whatever GameObject that script is attached to. If you're trying to find a component on something else then this won't work. It's not "clearing for no reason". The method returns null if it can't find what you're looking for.

    @Dantus was pointing out that you're defining DoorPresets as a doorpreset but then assigning it a value from the preset enum. That doesn't make any sense and it's amazing that it compiles in the first place
     
  10. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I will post a large excerpt from our script.
    Please bare in mind that this is part of a commercial asset and is NOT in anyway me giving it out.
    This is just a small part of it anyway.
    I have cut a piece out of the script (the top bit using the enum).
    I should mention it was all working before I started adding the Enum thing.

    Door Script
    Code (JavaScript):
    1.     /*
    2.     Scene Doors by Kyle Briggs 05/02/2015
    3.     (C) Labyrith Studios, Kyle Briggs 2015
    4.     --------------------------------------
    5.     Version 1.1f Changelog
    6.  
    7.     -C# Versions Introduced
    8.     -Unity 4.6+ GUI Support
    9.     --------------------------------------
    10.     Version 1.0f Changelog
    11.    
    12.     -Increased Support for devices
    13.     -Support for same-scene locations
    14.     -Commented Code
    15.     -Easier GUI Layout
    16.     -Code Re-Optimisation
    17. */
    18. import UnityEngine.UI;
    19.  
    20. //variables
    21. enum doorpreset { Default, OnMouseLook }
    22. var DoorPresets : doorpreset;
    23. var isinTrigger : boolean = false;
    24. var Door : MouseDetect;
    25.  
    26. var PlayerCharacter : Transform; //Player
    27. var toText : String = ""; //What you want the target location to say on-screen
    28. var SpawnPoint : String = ""; //The name of the object you want to spawn the PlayerCharacter at
    29. var NewScene : String = ""; //The name of the scene you wish to go to
    30.  
    31. var GUIParent : GameObject; //This is where your GUI parent goes
    32. var text : Text;
    33. private var drawGUI = false; //Control for the GUI group layout
    34. private var hasToBeDestroyed = false; //Controls when the trigger gets destroyed
    35. function Start ()
    36. {
    37.     text.text = "";
    38. }
    39.    
    40. //Key control for initial teleportation  
    41. function Update ()
    42. {
    43.     if (drawGUI == true && Input.GetKeyDown(KeyCode.F))
    44.     {
    45.          LoadNewScene();
    46.     }
    47. }
    48.  
    49. //Trigger detection for Player tag
    50.  
    51. function OnTriggerEnter (theCollider : Collider)
    52. {
    53.     isinTrigger = true;
    54.     if (theCollider.tag == "Player")
    55.     {
    56.         if (DoorPresets == doorpreset.Default)
    57.         {
    58.         drawGUI = true;
    59.         // GUI setup
    60.         GUIParent.SetActive (true);
    61.         text.text = "" + toText;
    62.         }
    63.        
    64.         if (DoorPresets == doorpreset.OnMouseLook && isinTrigger && Door.MouseOn)
    65.         {
    66.             drawGUI = true;
    67.             // GUI setup
    68.             GUIParent.SetActive (true);
    69.             text.text = "" + toText;
    70.         }
    71.     }
    72. }
    73.  
    74. //function for loading the new level, setting the objects not to be deleted
    75.  
    76. function LoadNewScene ()
    77. {
    78.     // Don't destroy the player
    79.     DontDestroyOnLoad(PlayerCharacter);
    80.     // Don't destroy this trigger because we want it to handle the teleportation of the player
    81.     DontDestroyOnLoad(gameObject);
    82.     // When the level will be loaded, we must know which trigger has to be destroyed
    83.     hasToBeDestroyed = true;
    84.     // Load the new scene ; a room or "outside"
    85.     Application.LoadLevel(NewScene);
    86. }
    87. // Called once the level is fully loaded
    88. function OnLevelWasLoaded ()
    89. {
    90.     if (hasToBeDestroyed)
    91.     {
    92.         // We need to find the right spawn point (so it MUST have a unique name)
    93.         var spawner = GameObject.Find (SpawnPoint);
    94.         // Teleport the player to the spawn point
    95.         PlayerCharacter.position = spawner.transform.position;
    96.         // Destroy the trigger because we don't need it anymore
    97.         Destroy(gameObject);
    98.     }
    99. }
    100. function inTrigger()
    101. {
    102.     if (drawGUI == true)
    103.     {
    104.         // GUI setup
    105.         GUIParent.SetActive (true);
    106.         text.text = "" + toText;
    107.     }
    108.     else
    109.     {
    110.         GUIParent.SetActive (false);
    111.     }
    112. }
    113.  
    114. //Removal of GUI draw when exiting the trigger
    115.  
    116. function OnTriggerExit (theCollider : Collider)
    117. {
    118.  
    119.     isinTrigger = false;
    120.     if (theCollider.tag == "Player")
    121.     {
    122.         if (DoorPresets == doorpreset.Default)
    123.         {
    124.         drawGUI = false;
    125.         GUIParent.SetActive (false);
    126.         }
    127.        
    128.         if (DoorPresets == doorpreset.OnMouseLook && isinTrigger && Door.MouseOn)
    129.         {
    130.             drawGUI = false;
    131.             GUIParent.SetActive (false);
    132.         }
    133.     }
    134. }
    MouseDetect
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. public var MouseOn : boolean;
    4.  
    5. function OnMouseOver ()
    6. {
    7.     MouseOn = true;
    8. }
    9.  
    10. function OnMouseExit ()
    11. {
    12.     MouseOn = false;
    13. }
     
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Welcome to dynamic typing. Very, very easy to get wrong if you don't know what you are doing. Especially because the underlying mono/.NET framework doesn't actually support it. Trying to run a dynamically typed language on top of a statically typed framework was a mistake on Unity's part. And its the ultimate reason why good JavaScript ends up harder then good C#. Its also not documented overly well.

    You should probably throw #pragma strict at the top of all of your scripts. It will tell the compiler to check your typing. It will cause a lot of compiler errors, but after you fix them your code should work much better. Its that or find each error once it occurs at runtime.
     
    ATLAS-INTERACTIVE likes this.
  12. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    @BoredMormon I agree with this, it was a mistake to try and mix dynamic language and static .net, but it's a bit too late for that now.
    I have posted a large excerpt from my script above, if anyone can help try to fix it, I would be eternally grateful, several of our other scripts are mapped out like this, so once this one is fixed, I can use this as a reference to update the others.
     
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    What happened when you put #pragma strict at the top?

    My JavaScript is not strong enough to make coding in it a pleasant experience for me.
     
  14. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    I ported 3500 lines of js into c# and it took me an afternoon. I have never regretted making the switch to (and remaining with) c# from then on.

    Re:Code
    Are you sure that the error is somewhere in the provided snippet? What is the error now?
    One thing I noted on a quick skim, line 119, you have isinTrigger = false before verifying what it was that exited the trigger, then just after, on line 128, you test for isinTrigger which will invariably be false.
     
  15. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    The problem at the moment is that even with the code as it is, when I look at the object and in the collider, the text is never set to
    text.text = "" + toText;
    It just remains blank.