Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by Ochreous · Nov 02, 2013 at 02:11 AM · c#variableif-statements

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;
 }

 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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]);
         }
     }
 }
 
Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ochreous · Nov 02, 2013 at 05:57 AM 0
Share

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.

avatar image clunk47 · Nov 02, 2013 at 06:07 AM 1
Share

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.

avatar image fafase · Nov 02, 2013 at 06:12 AM 1
Share

And get ready for a few warnings.

avatar image clunk47 · Nov 02, 2013 at 06:16 AM 1
Share

@fafase yes sir, I'd say just use

 #pragma warning disable 0414 

But I would be a douche since that would be inefficient XD

avatar image fafase · Nov 02, 2013 at 06:21 AM 2
Share

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.

Show more comments
avatar image
0

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
 
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image clunk47 · Nov 02, 2013 at 06:05 AM 1
Share

I'd use a long ins$$anonymous$$d of a float if you're going to have a ton of digits.

avatar image Fornoreason1000 · Nov 02, 2013 at 06:28 AM 1
Share

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.

avatar image clunk47 · Nov 02, 2013 at 06:30 AM 1
Share

Just trying to point you in the right direction if you were to actually USE this example. ;)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges