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 /
avatar image
0
Question by CaptainPickle77 · Feb 05, 2019 at 03:14 AM · collisioninstantiatestatic

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);
     }
 }

}

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

1 Reply

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

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

Comment
Add comment · Show 7 · 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 CaptainPickle77 · Feb 05, 2019 at 11:57 AM 0
Share

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

avatar image MartinIsla · Feb 05, 2019 at 12:29 PM 0
Share

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.

avatar image CaptainPickle77 MartinIsla · Feb 05, 2019 at 12:53 PM 0
Share

ok that's fine

avatar image CaptainPickle77 MartinIsla · Feb 05, 2019 at 02:59 PM 0
Share

Ok so I have tried multiple things that you could've referenced and none of them have worked

avatar image MartinIsla CaptainPickle77 · Feb 05, 2019 at 03:45 PM 0
Share

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!

Show more comments

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

168 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

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


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