0

I have been having an issue with using anything like #region, #endregion, #if, #endif, etc in Unity lately.

I can't remember exactly what version of Unity this started with, but whenever I create a new project, I can't use regions at all.

It always says there is a parsing error, and then says something like this "error CS1027: Expected `#endif' directive"

To get that error, this is all I put

#if !UNITY_EDITOR
#endif

It doesn't matter if I include code between the statement, or strip all empty space around both sides of the directives..

I have another, older project, that I can use #regions, and #if statements in just fine, not really sure what changed or how to fix it.. I've been googling for solutions and it seems like no one else is having this problem? Is it a setting in monodevelop? White space? Invalid characters somewhere? I really have no idea why it is happening and it is driving me mad, haha.

If anyone has any suggestions, I would love to hear them!

Thanks for your time!

EDIT: Here is a example of #regions not working for me in a drag and drop script.. (BONUS, FREE DRAG AND DROP SCRIPT! LOL) Maybe just comment out the regions if they are giving you errors.. I had to. :(

Unity Console Errors:

Assets/Scripts/DragAndDrop.cs(12,254): error CS1028: Unexpected processor directive (no #region for this #endregion) (this one points me to the end of the summary tag?) Assets/Scripts/DragAndDrop.cs(14,45): error CS1028: Unexpected processor directive (no #region for this #endregion) ... etc

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// DragAndDrop.
/// This class will be responsible for listening to drag
/// events on the gameobject.
/// It will handle what happens during each drag state.
/// </summary>

public class DragAndDrop : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerDownHandler, IPointerUpHandler{
public RectTransform canvas;        // the uGui canvas in your scene

#region DRAG BOOLEANS
public bool canDrag = true;         // can this object be dragged?
public bool wasDragged = false;     // was this object recently dragged?
public bool isDragging = false;     // is object currently being dragged
public bool dragOnSurfaces = true;  
#endregion

#region SWIPE
public float comfortZoneVerticalSwipe = 50;     // the vertical swipe will have to be inside a 50 pixels horizontal boundary
public float comfortZoneHorizontalSwipe = 50;   // the horizontal swipe will have to be inside a 50 pixels vertical boundary
public float minSwipeDistance = 14;             // the swipe distance will have to be longer than this for it to be considered a swipe

public float startTime;     // when the touch started
public Vector2 startPos;    // where the touch started
public float maxSwipeTime;  // if the touch lasts longer than this, we consider it not a swipe
#endregion

#region PRIVATE
private GameObject draggingObject;
private RectTransform draggingTransform;
#endregion

#region UNITY CALLBACKS
void Awake(){
    canvas = GameObject.Find("Canvas").GetComponent<RectTransform>();
}
#endregion

#region TOUCH EVENTS
public void OnPointerDown(PointerEventData eventData){
    if(canDrag){
        wasDragged = false;
        // make sure object is parent to the canvas or it will disappear when picked up
        // I had to do this in word addiction because the letters were parented to tiles
        gameObject.transform.SetParent(canvas);
        // scale up when touched
        gameObject.transform.localScale = new Vector3(2, 2, 2);
    }
}

public void OnPointerUp(PointerEventData eventData){
    if(canDrag){
        // scale back down
        gameObject.transform.localScale = new Vector3(1, 1, 1);
    }
}
#endregion

#region DRAG EVENTS
public void OnBeginDrag(PointerEventData eventData){    
    if(canDrag){
        // start listening for swipe
        startPos = eventData.position;
        startTime = Time.time;

        // set drag variables
        isDragging = true;
        wasDragged = true;

        // run pick up logic
        PickUp(eventData);
    }
}

public void OnDrag(PointerEventData eventData){
    if(canDrag){
        if(draggingObject != null){
            Move(eventData);
        }
    }
}

public void OnEndDrag(PointerEventData eventData){
    if(canDrag){
        // swipe detection
        bool shouldFlick = false;
        float swipeTime = Time.time - startTime;
        float swipeDist = (eventData.position - startPos).magnitude;
        if (swipeTime < maxSwipeTime &&
            swipeDist > minSwipeDistance){
            shouldFlick = true;
        }
        // handle swipe/dropping
        if (shouldFlick){
            Debug.Log("FLICK");
        }else{
            isDragging = false;
            Place();
        }
    }
}
#endregion

#region EVENT FUNCTIONS
void PickUp(PointerEventData eventData){
    draggingObject = gameObject;
    Move(eventData);
}

void Move(PointerEventData eventData){
    if(dragOnSurfaces && eventData.pointerEnter != null && eventData.pointerEnter.transform as RectTransform != null){
        draggingTransform = eventData.pointerEnter.transform as RectTransform;
    }

    var rt = draggingObject.GetComponent<RectTransform>();
    Vector3 globalMousePos;
    if(RectTransformUtility.ScreenPointToWorldPointInRectangle(draggingTransform, eventData.position, eventData.pressEventCamera, out globalMousePos)){
        rt.position = globalMousePos;
        rt.rotation = draggingTransform.rotation;
    }
}

void Place(){
    Vector2 pos = new Vector2(transform.position.x, transform.position.y);
    Collider2D[] cols = Physics2D.OverlapCircleAll(pos, 10);
    float closestDistance = 0;
    GameObject closest = null;
    foreach(Collider2D col in cols){
        if(col.tag == "SomeTagToCheckFor"){
            Vector2 otherPos = new Vector2(col.transform.position.x, col.transform.position.y);
            if(closest == null){
                closest = col.gameObject;
                closestDistance = Vector2.Distance(pos, otherPos);
            } else{
                // here we will check to see if any other objects
                // are closer than the current closest object
                float distance = Vector2.Distance(pos, otherPos);
                if(distance < closestDistance){
                    // this object is closer
                    closest = col.gameObject;
                    closestDistance = distance;
                }
            }
        }
    }

    // snap to the closest object
    if(closest != null){
        // if something was detected to snap too?
    } else{
        // return object back?
    }
}
#endregion
}
5
  • If the current VS configuration is "release" switch it to "debug". Commented Apr 29, 2016 at 21:28
  • Hey there! Thanks for the reply. I'm using Monodevelop and it is currently set to debug! Thanks for the idea. Commented Apr 29, 2016 at 21:31
  • Hey Joe! Yes in the console, and yeah I am working on getting an example. The answer you posted works fine for me, even in newer projects now. But the weird thing is that I have used these directives a lot, A LOT, I love region organizing my code. In a previous project they would just not work at all, but I made a new project to test in Unity 5.3.2f1, and regions seem to be working fine now? I'm grabbing a few examples now. Commented Apr 29, 2016 at 22:03
  • Quick note: they're not "Regions". They preprocessor directives. the #region thing is something else, just for organizing code. Commented Apr 29, 2016 at 22:04
  • Cool! I wasn't sure what to call those, I thought they all fell under the same name since they used "#", my ignorance! Thank you! :) Commented Apr 29, 2016 at 22:09

1 Answer 1

3

So, regarding the EXAMPLE YOU GIVE.

I pasted it to a file HybFacebookExtensions.cs in an ordinary Unity5 project. It works perfectly - no errors.

It is likely there's a problem with your Unity install.

Unfortunately, nobody would be able to guess what is wrong there. Do you have a second machine on hand to test on?


#if !UNITY_EDITOR
Debug.Log("YO");
#endif

is a correct example.

it's likely you accidentally changed from Debug to Release.

Note. You can very confusingly get those sort of errors,

if your code just has simple syntax errors.

It's quite annoying. The following example can cause such unusual errors:

public Class Teste()
{
.. you meant to put it in here ..
}
#if UNITY_EDITOR
#endif
Sign up to request clarification or add additional context in comments.

3 Comments

Okay, So I'm going to post a few pastebin scripts. This is the script I grabbed from a gentleman who was working on a way to integrate facebook into standalone unity builds. This is one of his scripts here pastebin.com/HN43ZjEM. I made a few comments on where the error says it is, and where it actually is. Unity sometimes has issues pointing me to the correct line too.
Okay! Thank you, I'll do that! I need to run a quick errand, I will be back in about 15 minutes, I opened up my game jam project where I was also having issues and I will provide my some more examples. Thanks again for your time, I really appreciate all the help! I want to nail down what this issue is.
You very well may be right about that. Not sure what else it could be. I posted a drag and drop script that was giving me #region errors in my question. The really weird thing is this - That same code, with the regions, is working perfectly in a different project. But when I took that code, and put it into a new unity project, I got all of those errors. I'm very lost. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.