a local variable 'texture' can not be declared in this scope because it will give a different mean to texture

2

I'm new to C # script in Unity3D I get an error in EditorFacebookMockDialog.cs :

  

a local variable 'texture' can not be declared in this scope because it will give a different mean to texture   how can I resolve 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();
        }
    }
}
    
asked by anonymous 08.11.2016 / 14:26

1 answer

2

You can not declare two variables with the same name ( texture ), even if one of them is within if .

Just change the name of 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;
    
08.11.2016 / 16:12