- Home /
Need my boolean to apply to the instance of my object instead of class
So what I want to do is: shoot a sphere that has a script on it to apply damage and explode on collision. I also want that bullet able to be charged up, but I don't want it to explode while it is being charged and is on the tip of the barrel, so I made a static boolean variable called isExpanding which returns true while the bullet is at the tip of the barrel. But my problem is that the bullets that have been fired will not call OnCollisionEnter while isExanding is equal to true, when I am expanding other bullets, so I want the variable to only apply to that instance of the bullet. Please Help. Here's my code: for the player which has movement and shooting
//Variables individual to each player
public static float player1DamagePercentage;
public static float player2DamagePercentage;
public GameObject pivot;
public GameObject bulletEmitter;
public GameObject bullet;
private GameObject bulletInstance;
private Rigidbody rb;
private Rigidbody bulletRB;
public static bool isExpanding = false;
private float jumpSpeed = 50;
private bool m_isAxisInUse = false;
private bool inAir = false;
private float rTriggerRaw;
private float moveHorizontal;
private float rsVertical;
private float rsHorizontal;
private float rTrigger;
private bool aButton;
private float xAxis;
private float yAxis;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if (gameObject.name == "Player 2")
{
moveHorizontal = Input.GetAxis("Horizontal1");
rsVertical = Input.GetAxis("Mouse Y1");
rsHorizontal = Input.GetAxis("Mouse X1");
rTrigger = Input.GetAxis("Right Trigger1");
aButton = Input.GetButton("A Button1");
xAxis = Input.GetAxis("Mouse X1");
yAxis = Input.GetAxis("Mouse Y1");
}
if (gameObject.name == "Player 1")
{
moveHorizontal = Input.GetAxis("Horizontal");
rsVertical = Input.GetAxis("Mouse Y");
rsHorizontal = Input.GetAxis("Mouse X");
rTrigger = Input.GetAxis("Right Trigger");
aButton = Input.GetButton("A Button");
xAxis = Input.GetAxis("Mouse X");
yAxis = Input.GetAxis("Mouse Y");
}
rb.velocity = new Vector3(40 * playerSpeed * moveHorizontal * Time.deltaTime, rb.velocity.y + 0, 0);
if (aButton && inAir == false)
{
rb.AddForce(0, jumpSpeed * 10f, 0);
inAir = true;
}
if (yAxis != 0 || xAxis != 0)
{
float joystickAngle = Mathf.Atan2(xAxis, yAxis) * Mathf.Rad2Deg;
pivot.transform.rotation = Quaternion.Slerp(pivot.transform.rotation, Quaternion.Euler(pivot.transform.rotation.x, pivot.transform.rotation.y, joystickAngle - 90F), Time.deltaTime * 5);
}
}
void Update()
{
if(gameObject.name == "Player 2")
{
rTriggerRaw = Input.GetAxisRaw("Right Trigger1");
}
if(gameObject.name == "Player 1")
{
rTriggerRaw = Input.GetAxisRaw("Right Trigger");
}
if (rTriggerRaw != 0)
{
if (m_isAxisInUse == false)
{
bullet.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
bulletInstance = Instantiate(bullet, bulletEmitter.transform.position, pivot.transform.rotation);
m_isAxisInUse = true;
}
isExpanding = true;
bulletInstance.transform.localScale += new Vector3(sizeIncreaseRate * Time.deltaTime, sizeIncreaseRate * Time.deltaTime, sizeIncreaseRate * Time.deltaTime);
if (bulletInstance.transform.localScale.x >= 1.5f || bulletInstance.transform.localScale.y >= 1.5f || bulletInstance.transform.localScale.z >= 1.5f)
{
bulletInstance.transform.localScale = new Vector3(1.5f, 1.5f, 1.5f);
}
if (m_isAxisInUse == true)
{
bulletInstance.transform.position = bulletEmitter.transform.position;
}
}
if (rTriggerRaw == 0 || Input.GetKeyDown("w"))
{
isExpanding = false;
if (bulletInstance != null)
{
bulletRB = bulletInstance.GetComponent<Rigidbody>();
if (bulletRB != null)
{
bulletRB.AddForce(pivot.transform.right * bulletSpeed, ForceMode.VelocityChange);
Destroy(bulletInstance, 2f);
m_isAxisInUse = false;
}
}
}
}
void OnCollisionEnter(Collision ground)
{
if(ground.collider.tag == "Ground")
{
inAir = false;
}
}
Here's the code for the bullet which has all the collision detection on it
public GameObject pivot;
public GameObject explosionEffect;
private Rigidbody collisionRB;
void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == ("Player") && PlayerMovement1.isExpanding == false)
{
collisionRB = collision.collider.GetComponent<Rigidbody>();
collisionRB.AddForce(transform.right * 100, ForceMode.Force);
GameObject explosionInstance = Instantiate(explosionEffect, transform.position, transform.rotation);
Destroy(gameObject);
Destroy(explosionInstance, 1f);
}
if(collision.collider.tag == ("Ground") && PlayerMovement1.isExpanding == false)
{
Destroy(gameObject);
}
}
}
Answer by MartinIsla · Feb 05, 2019 at 04:33 AM
Hi, good evening.
The problem here is a misunderstanding of what static variables are for.
Static variables have always the same value for every object because it doesn't really belong to an object, but to the actual class. This means that if isExpanding is true, it will be true for every sphere in the game. Also, as a general rule -and this is just my advice and opinion- stay away from static variables in Unity as much as possible. They will not behave the way you expect them to most of the time.
Your only option here is to change the way the bullet knows if it's "expanding". What I would do is to actually assign the "isExpanding" value in the sphere from the Player script. Move the isExpanding variable to the bullet script and make it public and not static.
When expanding, do GetComponent().isExpanding = true.
Hope this helps!
Happy game development
Thank you for your response, that was very helpful, but is there any way you could show me what you are talking about. I am rather new to Unity and program$$anonymous$$g in general. Thanks
Hey, I can give you hints, but you should do it yourself. That’s the only way to learn! If I just tell you what to write, you’d learn nothing from it.
Ok so I have tried multiple things that you could've referenced and none of them have worked
No worries, let me help you a bit further. There's a part of your code where you instantiate the ball and after that, you set isExpanding to true in the player.
Ins$$anonymous$$d of doing that, you move the isExpanding variable to the Bullet script (if you don't have a Bullet script, you need a Bullet script attached to the bullet prefab!) and make it public. Then you can do bulletInstance.GetComponent().isExpanding = true.
When that's done, when checking OnCollisionEnter, ins$$anonymous$$d of checking Player$$anonymous$$ovement1.isExpanding, you check collision.gameObject.GetComponent().isExpanding!
Your answer
Follow this Question
Related Questions
Need help on spawning of objects from random brick in a brick breaker game 0 Answers
Stop instantiate on collision from other script 1 Answer
Instantiated objects ignoring collisions randomnly o.O 2 Answers
Problem with Static Variable? 1 Answer
How to make objects spawn at the same time without colliding ? 1 Answer