Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Mar 18, 2018 at 11:53 AM by $$anonymous$$ for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by $$anonymous$$ · Mar 05, 2018 at 10:21 AM · scripting problemvariablegameobjectsinstances

Multiple instances of an object referencing a variable in a script but having different values?

I'm trying to create a two-player tank battling game and have come a bit stuck. For each tank I want to track the number of bullets left to prevent them from firing too many. Each instance of the tank is referring to a single script called TankShooting, inside of which is a variable called bulletCount that I use to track the number of bullets remaining.
My question is this: Is there a way for me to assign this variable a different value for each of the Tank instances in the scene?
For example, I'll start by giving tanks Tank1 and Tank2 three bullets each. If Tank1 fires a bullet, I want to decrease its value of bulletCount by 1 , while keeping the value of bulletCount for Tank2 the same (i.e. 3).
Thank you so much if anyone is able to help me out with this. I really appreciate it!

Here's a copy of my TankShooting script:

 using UnityEngine;  
 using UnityEngine.UI;
 
 public class TankShooting : MonoBehaviour
 {
     public int m_PlayerNumber = 1; // Used to identify which player called the script
     public Rigidbody m_Shell; // The prefab of the shell
     public Transform m_FireTransform;
     public float m_LaunchForce = 30f;
     
     private string m_FireButton;        
     private bool m_Fired;
     public int m_BulletCount = 3;
 
     private void OnEnable()
     {
         
     }
 
     private void Start()
     {
         Debug.Log ("Bullet count at start = " + m_BulletCount);
         m_FireButton = "Fire" + m_PlayerNumber;
     }
 
     private void Update()
     {
         if (Input.GetButtonDown (m_FireButton)) {
             Fire ();
         }
     }
 
 
     private void Fire()
     {
         if (m_BulletCount > 0) {
             // Instantiate a shell and launch it.
             Rigidbody shellInstance = Instantiate (m_Shell, m_FireTransform.position, m_FireTransform.rotation) as Rigidbody;
 
             // Set the shells velocity to the value of m_LaunchForce in the direction the tank is facing
             shellInstance.velocity = m_LaunchForce * m_FireTransform.forward;
         } else {
             Debug.Log ("Not enough bullets left to fire!");
         }
     }
 
     public int getBulletCount() // Returns the current value of m_BulletCount as an integer
     {
         return m_BulletCount;
     }
 
     public void setBulletCount(int value) // Sets m_BulletCount to a specific value
     {
         m_BulletCount = value;
     }
 
     public void incrementBulletCount(int value) // Increment m_BulletCount by the increment specified
     {
         Debug.Log (m_BulletCount);
         m_BulletCount = m_BulletCount + value;
     }
 }
Comment
Add comment · Show 4
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 Hellium · Mar 05, 2018 at 10:56 AM 0
Share

Why do both tank are referring to the same instance of TankShooting?

avatar image $$anonymous$$ Hellium · Mar 06, 2018 at 05:56 PM 0
Share

I'm not the best at Unity, so it could just be me misunderstanding how it works but I've set both Tanks to reference one script called TankShooting. I'm not sure if it's the same instance of the script, though.

avatar image Hellium $$anonymous$$ · Mar 07, 2018 at 07:27 AM 0
Share

Do you mean you have attached the script TankShooting to both gameobjects? If so, they have their own instance.

Please, provide the TankShooting script to understand what may be wrong.

avatar image Hellium · Mar 07, 2018 at 11:11 AM 0
Share

Your script seems fine.

Who calls the getBulletCount, setBulletCount and incrementBulletCount?

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Giannigiardinelli · Mar 07, 2018 at 08:06 AM

Hi, I think Hellium to reason shows us your script to better understand. As he told you he sufi the script in each tank.

 private GameObject bullet;
     private GameObject gunModel;
 
     public float NumBullet = 1; // One shot for each bullet in the variable MaxBullet
     public float bulletMax = 5;
     public bool NoBullet = true;
 
     void Update()
     {
 
         if (bulletMax < 0) {
 
             NoBullet = false;
 
         }
         if (Input.GetMouseButtonDown(0))
         {
             if (NoBullet) {
                 GameObject bullet = Instantiate (bullet);
                 bullet.GetComponent<Rigidbody> ().AddForce (gunModel.transform.forward * 700);
                 bulletMax -= NumBullet  ;
             }
 
         }
     }

For example this script can just serve the two tanks, if not what you want to do, send us more information to soon

Comment
Add comment · Show 1 · 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 $$anonymous$$ · Mar 07, 2018 at 10:49 AM 0
Share

I've updated my post with the TankShooting code. Thank you for helping me out :)

Follow this Question

Answers Answers and Comments

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

Related Questions

Setting GameObject properties in editor when public script fields are changed 2 Answers

Change in Editor via Script Values of another Script 2 Answers

Add a script to a GameObject via script at runtime? 1 Answer

Getting wrong value of variable 1 Answer

Adjust the size of the object within a parent container 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