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 camtronius · Apr 25, 2014 at 04:06 AM · enemymultiplertsindexunits

How to index objects

Hi guys, im not sure if this is the correct terminology. But what I am trying to do is.. I have a health bar connected to a tower. When a unit is near it decreases. Works great. But when I have multiple of the same tower on the screen its buggy. I think its because I am using the same variable for both towers. How do I make it so their variables are not shared? I have my code below with comments. It is applied to the "towers" in my game.

 using UnityEngine;
 using System.Collections;
 
 public class Tower : MonoBehaviour
 {
     private int testInt=0;    // Update is called once per frame
 
     public Texture2D selectionHighLight = null; 
     private Vector3 captureBarPos = Vector3.zero;
 
     private static float captureBarLength = 500f;
     private static float maxCaptureBarLength =500f;
     private static float length = 50f;
 
     public bool playerCapture =false;
 
     void Update(){
 
         captureBarPos = Camera.main.WorldToScreenPoint (transform.position);
         print("target is " + captureBarPos.x + " pixels from the left"); 
 
         if(captureBarLength>=0){ //this is the code that captures faster based on amt of units in area
         switch(testInt){
         case 1: captureBarLength-=1;
             break;
         case 2: captureBarLength-=2;
             break;
         case 3: captureBarLength-=3;
             break;
             }
             //test
             if(captureBarLength<3){//just a test to see color change
                 playerCapture=true;
             }else{
                 playerCapture=false;
             }
 
         }
         if (captureBarLength <= maxCaptureBarLength && testInt == 0) { //refill bar when no units around
             captureBarLength+=5;        
         }
         if (playerCapture == true) {
                         renderer.material.color = Color.red; //change color when captured
                 } else {
             renderer.material.color = Color.white;
                 }
 
     }
 
 
     void OnTriggerEnter(Collider col) { //detecting if "unit" is in the area 
             if(col.collider.gameObject.name=="Unit"){
             testInt+=1;
 
             Debug.Log(testInt);
             }
         } 
     void OnTriggerExit(Collider col) { //detecting if "unit" is in the area 
         if(col.collider.gameObject.name=="Unit"){
             testInt-=1;
             Debug.Log(testInt);
         }
 
     }
     private void OnGUI(){
         GUI.DrawTexture(new Rect (captureBarPos.x-30,(Screen.height-captureBarPos.y)-50,length*(captureBarLength/maxCaptureBarLength),5),selectionHighLight);    
     }
 }







Comment
Add comment · Show 1
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 getyour411 · Apr 25, 2014 at 04:07 AM 0
Share

I don't see a healthBar in that code but I see static variables at the top which coupled w/ the problem description sounds like the culprit.

1 Reply

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

Answer by jjmontes · Apr 25, 2014 at 04:52 AM

Seems like your variable captureBarLength belongs to the Tower instance, and hence shall not be static. Remember that static variables are shared among all instances of a class (in this case Tower).

Not sure about your other static vars, double check whether they are global, or remove the static modifier from all to be on the safe side.

Comment
Add comment · Show 2 · 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 camtronius · Apr 25, 2014 at 03:10 PM 0
Share

@jjmontes Ill try making it not static and see what happens. I made it static because im trying to make my health bar based on percentages. I want to keep the same size health bar, no matter how much the health value is. This was posing a problem when I tried to run the game. Without static int's the bar would be like off the screen in length. Do you know how I could do this? Thanks,

 length*(captureBarLength/maxCaptureBarLength)

length is the bar length, captureBarLength is what decreases, maxCaptureBarLength is how much health would need to be decreased for capture

avatar image jjmontes · Apr 29, 2014 at 02:15 PM 0
Share

Ok that you use a static field for maxCaptureBarLength. But you are using length*(captureBarLength/maxCaptureBarLength) for the Rect width, and the three variables are static. I don't fully grasp what you are trying to achieve, but doesn't look right: your bar width calculation must refer to some local field (non-static) which contains the value health. Ultimately you want to normalize that health to a float between 0 and 1 in order to multiply your maxCaptureBarLength.

Out of intuition, in your case I'd normally use something like healthNormalized * maxCaptureBarLength, and health must not be static (after all it has a different value for each tower).

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

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

Related Questions

RTS Movement and grid questions 0 Answers

How to add gameobject inside of an array? 0 Answers

Get First Object In List 2 Answers

RTS Unit Creation Bar Queue System 1 Answer

rts developement 0 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