C# Declaring Variables Within If Statements

I was wondering is it possible to declare a variable within an if statement? I have a gameobject that I would like to have only certain variables.

public string someString;

void Update(){
if(somestring == "x"){
public float someFloat;
}

You can’t declare a public local variable, but you could just use someFloat in the same way you did someString.

public string someString;
public float someFloat;

void Update()
{
    if(someString = "X")
        someFloat = 24;
}

Not sure what you’re going for, but I’ll show you an array example just to hopefully address the question appropriately.

In this example, you can do completely random numbers and letters, but I’ll use the alphabet in order just to help you understand what exactly I’m doing. What I’m doing here is using 26 letters (the alphabet) and creating an int array from it. A = 0, B = 1, and so on.

using UnityEngine;
using System.Collections;

public class AlphaNumTest : MonoBehaviour 
{
	string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	int[] num;
	
	void Start()
	{
		num = new int[alpha.Length];
		for(int i = 0; i < num.Length; i++)
		{
			num *= i;*

print (alpha_+" = "+num*);
}
}
}*_

remove the public modifier or make it a MemeberWise variable;

public string someString;
 
void Update(){
if(somestring == "x"){
float someFloat; //public modifier not needed as it is  local variable                                             
}

//or

public string someString;
 private float someFloat;
void Update(){
if(somestring == "x"){
somefloat = 129083409759845.13452345f; //float assigned a stupid number

}