The question is answered, right answer was accepted
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;
}
}
Why do both tank are referring to the same instance of TankShooting
?
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.
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.
Your script seems fine.
Who calls the getBulletCount
, setBulletCount
and incrementBulletCount
?
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
I've updated my post with the TankShooting code. Thank you for helping me out :)
Follow this Question
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