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
0
Question by Afropenguinn · Oct 02, 2013 at 04:50 AM · variablesscope

Variable Scope

Why is it that my variable "attack" has a scope that extends into the "Attack()" function, but my variable "target"'s scope does not reach into the "GetTarget()" function? Just curious.

 using UnityEngine;
 using System.Collections;
 
 public class MonsterAI : MonoBehaviour
 {
     public string name = "NoName";
     public GameObject target = null;
     public float hp = 100f;
     public float attack = 10f;
     public float speed = 100f;
     public float team = 1f;
     
     public float action_bar = 0f;
     
     void Start()
     {
     
     }
     
 
     void Update()
     {
         //ticking down their action bar
         if (action_bar < 100f)
         {
             action_bar += speed * Time.deltaTime;
         }
         else
         {
             target = GetTarget ();
             Attack ();
             Debug.Log (name + " has ended their turn.");
             target = null;
             action_bar = 0f;
         }
         
         //death
         if (hp <= 0)
         {
             Destroy (gameObject);
         }
     }
     
     //functions
     GameObject GetTarget()
     {
         GameObject[] target = GameObject.FindGameObjectsWithTag("Monster");
         return (target[0]);
     }
     
     void Attack()
     {
         string target_name = target.GetComponent<MonsterAI>().name;
         float damage = Mathf.Round(Random.Range (attack - (attack * .1f), attack + (attack * .1f)));
         target.GetComponent<MonsterAI>().hp -= damage;
         Debug.Log (name + " attacks " + target_name + " for " + damage + "damage.");
     }
     
     void Dodge()
     {
         
     }
 }
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
1
Best Answer

Answer by whydoidoit · Oct 02, 2013 at 05:03 AM

It's because you are also defining a variable called "target" in GetTarget which hides the one from the outer scope.

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 Afropenguinn · Oct 02, 2013 at 05:19 AM 0
Share

Ah so I can "mask" a variable with another variable? That does sound like bad practice though, would you guys agree? Seems like it would be confusing to whomever picked up my code.

avatar image whydoidoit · Oct 02, 2013 at 05:25 AM 0
Share

Generally speaking it's very confusing, so you should avoid it. If you use Visual Studio and Resharper it complains a lot about it and points it out to you.

You could choose longer variable names, which helps self document the code (and given intellisense is a lot less typing than you might imagine).

avatar image Fattie · Oct 02, 2013 at 08:33 AM 0
Share

"Ah so I can "mask" a variable with another variable?" - yes, simply never do it, it's that simple. You're fortunate to have @whydo answer a question, everyone else here is drunk :)

avatar image
0

Answer by himanshugupta159 · Jan 25, 2019 at 07:36 AM

Written a blog related to the question which gives the indepth information related to varible scope. link:https://unfragilecoding.blogspot.com/2019/01/variable-scope.html

Comment
Add comment · 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

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

17 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 avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Defining a variable in another script? 1 Answer

Public variable in script different for every game object. 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Get Variable From A Script Attached To a Prefab 3 Answers


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