Equivalent of "isset" (PHP) in C# Unity

isset in PHP basically allows you to check if a variable == null without yelling an error that a variable doesn’t exist. Is there an equivalent for that in C# unity?

I tried
if (MyVariable != null)
but it yelled out an error.

Helpsies?

Can you post your code because that’s definitely how you do it.

Eg.

Int? Nullableint;
If(Nullableint == null)
{
  //Do stuff
}

You don’t even need the “!= null” part really, it should only yell out an error if you’re trying to compare null and a value type variable (or a null reference type with a value other than null).

Edit: I see, you meant uninitialized variables (misread, or just tired). There’s no way to check for such a thing because it really shouldn’t happen. When you have public reference-type class members they initialize themselves to null if no value is assigned in the Inspector, I think. For local variables you should be assigning them a default or initial value (usually null) right away. Then you can simply compare them to null to find out if they’ve been assigned.

But if the variable isn’t defined as a variable anywhere in the code, it still shouts out an error

I set MyAnswer as a static string variable in another code

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;  //its a must to access new UI in script

public class TextScript : RandomObjects
{
    public List<string> newrandomobjects = new List<string>();
    public string gametext;
    public Text UIText; // assign it from inspector
    void Start()
    {
        newrandomobjects = ReorderList ();
        UIText.text = "Pick the "+newrandomobjects[0];
    }

    void OnMouseDown()
    {
        if (MyAnswer != null) {
            if (newrandomobjects [0] == MyAnswer) {
                UIText.text = "Your Answer is Correct";
            } else {
                UIText.text = "Your Answer is Wrong";
            }
        }
    }
}

So the error was that MyAnswer wasn’t recognized because you don’t have a reference to it.

You need to do something like:

if(OtherClassWhereMyAnswerIs.MyAnswer != null)

Yes, you’re right (and thank you), but that leads me to another problem. Since OnMouseDown is a void I can’t apply a return value to it, which means that MyAnswer loses its value after the OnMouseDown void function is finished, so I can’t really apply that static string to other classes, right?

If so, what’s a good alternative to void OnMouseDown in that case?

using UnityEngine;
using System.Collections;

public class TriviaClick : MonoBehaviour {
    public static string MyAnswer;
    // Use this for initialization
    void OnMouseDown () {
        MyAnswer = gameObject.name;
    }
}

It definitely shouldn’t lose it’s value just because the function ends.

You should take a step back and brush up on your C# basics.

I’m looking for something specific, I’m not gonna start reviewing all the past C# basic material because I’ll never get ahead like that.

Anywho, while MyAnswer exists after “void” in the same class, I think that if I could find a way to save that MyAnswer value that’d be great. Unfortunately I’m unable to use the return after the void OnMouseDown… I’m a little bit lost where do I go from here.

Absolutely incorrect. You’re making half a dozen fairly fundamental mistakes in your thinking of C# code in general and it’s making it very hard for us to explain where and how your scripts are wrong. You’ll never get ahead by skipping the basics. You need to go find a C# book and sit down with it for a few days so you can gain a basic understanding of how the language works.

I haven’t skipped the basics, I’m here to learn and I am working with a book. A book can’t answer questions, you can, if you got the right attitude and patience.

If my assumptions are wrong just tell me why. If it’s too difficult for you to explain, then don’t, I didn’t ask for a lecture.

OK, the other script does recognize and print the “MyAnswer” variable. Everything works! Thank you^^.