the local variable `Texture' cannot be declared in this Scope because it will Give a Different Mean to Texture

Asked

Viewed 71 times

2

I’m new to c# script in unity3d I get an error in EditorFacebookMockDialog.cs:

the local variable `Texture' cannot be declared in this Scope because it will Give a Different Mean to Texture how can I fix this error?

The code

namespace Facebook.Unity.Editor
{
    using System;
    using System.Collections.Generic;
    using UnityEngine;

    internal abstract class EditorFacebookMockDialog : MonoBehaviour
    {
        public delegate void OnComplete(string result);

        public OnComplete Callback { protected get; set; }

        public string CallbackID { protected get; set; }

        protected abstract string DialogTitle { get; }

        public void OnGUI()
        {
            if ( this.modalStyle == null )
            {
                this.modalRect = new Rect(10, 10, Screen.width - 20, Screen.height - 20);
                this.modalStyle = new GUIStyle(GUI.skin.window);
                Texture2D texture = new Texture2D(1, 1);
                texture.SetPixel(0, 0, new Color(0.2f, 0.2f, 0.2f, 1.0f));
                texture.Apply();
                this.modalStyle.normal.background = texture;
            }

            var rect = new Rect(10, 10, Screen.width - 20, Screen.height - 20);
            Texture2D texture = new Texture2D(1, 1);
            texture.SetPixel(0, 0, new Color(0.2f, 0.2f, 0.2f, 1.0f));
            texture.Apply();
            var style = new GUIStyle(GUI.skin.window);
            style.normal.background = texture;


            GUI.ModalWindow(
                this.GetHashCode(),
                rect,
                this.OnGUIDialog,
                this.DialogTitle,
                style);
        }

        protected abstract void DoGui();

        protected abstract void SendSuccessResult();

        protected void SendCancelResult()
        {
            var dictionary = new Dictionary<string, object>();
            dictionary[Constants.CancelledKey] = true;
            if (!string.IsNullOrEmpty(this.CallbackID))
            {
                dictionary[Constants.CallbackIdKey] = this.CallbackID;
            }

            this.Callback(MiniJSON.Json.Serialize(dictionary));
        }

        protected void SendErrorResult(string errorMessage)
        {
            var dictionary = new Dictionary<string, object>();
            dictionary[Constants.ErrorKey] = errorMessage;
            if (!string.IsNullOrEmpty(this.CallbackID))
            {
                dictionary[Constants.CallbackIdKey] = this.CallbackID;
            }

            this.Callback(MiniJSON.Json.Serialize(dictionary));
        }

        private void OnGUIDialog(int windowId)
        {
            GUILayout.Space(10);
            GUILayout.BeginVertical();
            GUILayout.Label("Warning! Mock dialog responses will NOT match production dialogs");
            GUILayout.Label("Test your app on one of the supported platforms");
            this.DoGui();
            GUILayout.EndVertical();

            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            var loginLabel = new GUIContent("Send Success");
            var buttonRect = GUILayoutUtility.GetRect(loginLabel, GUI.skin.button);
            if (GUI.Button(buttonRect, loginLabel))
            {
                this.SendSuccessResult();
                MonoBehaviour.Destroy(this);
            }

            var cancelLabel = new GUIContent("Send Cancel");
            var cancelButtonRect = GUILayoutUtility.GetRect(cancelLabel, GUI.skin.button);
            if (GUI.Button(cancelButtonRect, cancelLabel, GUI.skin.button))
            {
                this.SendCancelResult();
                MonoBehaviour.Destroy(this);
            }

            var errorLabel = new GUIContent("Send Error");
            var errorButtonRect = GUILayoutUtility.GetRect(cancelLabel, GUI.skin.button);
            if (GUI.Button(errorButtonRect, errorLabel, GUI.skin.button))
            {
                this.SendErrorResult("Error: Error button pressed");
                MonoBehaviour.Destroy(this);
            }

            GUILayout.EndHorizontal();
        }
    }
}
  • The questions here must be in English :) remember to add the language tag as well.

  • Even the error messages? For those who have difficulty with English this is a great deterrent.

  • @Cleiton But he wasn’t talking about the error messages...

1 answer

2

You cannot declare two variables with the same name (texture), even if one of them is inside the if.

Just rename one of the variables.

if (this.modalStyle == null)
{
    //...
}

var rect = new Rect(10, 10, Screen.width - 20, Screen.height - 20);
Texture2D texture2 = new Texture2D(1, 1);
texture2.SetPixel(0, 0, new Color(0.2f, 0.2f, 0.2f, 1.0f));
texture2.Apply();
var style = new GUIStyle(GUI.skin.window);
style.normal.background = texture2;
  • Instead of changing the name the best in this example would be to move the first statement up the if and remove the second.

  • Why is it better? Stop to analyze the code with your tip. If the result of modalStyle for null an object of the type Texture2D, there will be some modifications in the object and then modalStyle.normal.background will receive a reference to this object. Soon after, the object will be re-modified, that is to say, modalStyle.normal.background will have its properties changed. If you are going to do this, what is the sense of having the code block inside the if?

  • One was missing else to make sense of this condition. Note that the condition only treats a deviation from how this instance should be configured.

  • [...] Of course it lacks a little attention of the OP. should have a else in the code, or else the second block of code should set another property that was not style.normal.background, but that’s another problem.

  • True, the way it is gives room for interpretation.

  • Yes, @Cleiton. This is a great evil of badly written questions. Fortunately, in this case, it does not affect the "main scope" of the question, which is the problem of having two variables with the same name. We can only see (other) code flaws that are not pointed out(s) in the question.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.