- Home /
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;
}
Answer by clunk47 · Nov 02, 2013 at 03:02 AM
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] = i;
print (alpha[i]+" = "+num[i]);
}
}
}
Is there any harm declaring a variable and not using it? I'm going to have multiple instances of this gameobject and they won't be using some of the variables I've declared.
Needs to be defined if SO$$anonymous$$ETHING is using it. Will use some additional bytes of memory, so if you're not ever using a certain variable on ANYTHING, then don't declare it.
@fafase yes sir, I'd say just use
#pragma warning disable 0414
But I would be a douche since that would be inefficient XD
Conclusion for that question:
-If you do not plan on eating $$anonymous$$armite (place Vegemite here if you are Aussie), why are you buying some?
-But it does not hurt to have some in the cupboard.
-True but why buying it in the first place if you are not English (or Aussie)?
-Ok I leave it...
Same applies for variable.
Answer by Fornoreason1000 · Nov 02, 2013 at 02:49 AM
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
}
I'd use a long ins$$anonymous$$d of a float if you're going to have a ton of digits.
its an example... but yeah, anything over +/-4 billion id use long, but i never used anything over 275 million(super boss HP) for Games.
Just trying to point you in the right direction if you were to actually USE this example. ;)
Your answer
Follow this Question
Related Questions
C# Character Controller Collision If Statement 2 Answers
C# if If Statement is True Declare Variable 1 Answer
C# Accessing Variables from Class Variable 1 Answer
How to Name Individual Buttons Within a List 2 Answers
c# Adjust In-Game audio 1 Answer