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

Monodevelop not highlighting 'var' variable

Discussion in 'Scripting' started by popuppirate, Sep 15, 2014.

  1. popuppirate

    popuppirate

    Joined:
    Sep 15, 2014
    Posts:
    21
    Hi!

    I'm just starting with Unity and am getting familiar with Monodevelop. I've seen a few scripts that I'm using for practice using the 'var' variable (not seen it before, in my previous C++ experience I only really used void, double and bool) but the program doesn't highlight the variable in blue as with these.

    Is there a good reason for this? Have I missed something?

    Kind Regards,

    PopupPirate
     
  2. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    It doesn't? Well that's weird... It does for me.
     
  3. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I would suggest taking a screenshot and reporting it as a bug using the menu "Help | Report a Bug" menu inside Unity.
     
  4. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    Or maybe MonoDevelop is acting weird. Try closing it and re opening MonoDevelop. This had happened to me quite a few times and boy was it frustrating.
     
  5. popuppirate

    popuppirate

    Joined:
    Sep 15, 2014
    Posts:
    21
    Hi guys, thanks for the reply!

    Just for reference, here's a screenshot to show you what I mean.

    @numberkruncher I'll do that ASAP.

    @zDemonhunter99 I will try and do that. For reference, I'm opening the program from within Unity
     

    Attached Files:

  6. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Your syntax is completely invalid hence why the syntax colouring isn't as you would expect. This is not a bug with MonoDevelop.

    You can't have a "void" variable, the whole purpose of a variable is to hold some value. "var" only works for local variables:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour {
    5.  
    6.     // Member variable (lifetime of "Player" component).
    7.     public int someNumber = 42;
    8.     public string someString = "Hello World!";
    9.  
    10.     void Start() {
    11.     }
    12.  
    13.     void Update() {
    14.         // Local variable (lifetime of one single Update() call)
    15.         int tempNumber = 42;
    16.  
    17.         // Using the "var" keyword for primitive type:
    18.         var tempNumber2 = 42;
    19.  
    20.         // Using the "var" keyword for reference type:
    21.         var someOtherComponent = GetComponent<SomeOtherComponent>();
    22.     }
    23.  
    24. }
    Personally I would recommend only using the "var" keyword when working with references to objects. I find that "var" for primitive types makes code harder to read.
     
    popuppirate likes this.
  7. popuppirate

    popuppirate

    Joined:
    Sep 15, 2014
    Posts:
    21
    Cheers, I saw the var variable out of context then. Thanks very much!
     
  8. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Reiterating that a lot of times when Mono doesn't highlight a keyword properly, its because of a syntax error or some other compilation error somewhere else in the script. This also tends to cause problems with intellisense.