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 /
This question was closed Mar 31, 2016 at 09:24 PM by Spider-og for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Spider-og · Mar 31, 2016 at 07:16 PM · destroyontriggerenterfps controller

Why does my FPSController destroy when I collide?

When my player collides with the r_pickup it destroys the fps controller, and i have no idea why. Sorry if its a easy fix, still a beginner :).

BTW it also does this when i use OnCollisionEnter()

using UnityEngine;

public class AimAnim : MonoBehaviour {

 public static AimAnim aimAnimInstance;

 public AudioClip pickSound;

 bool isPickedUp = false;
 bool IsAiming = false;
 bool hasAmmo = true;
 bool canReload = false;
 bool isReloading = false;
 bool isDead = false;


 public AudioClip shootSound;

 public GameObject bullet;
 public GameObject spawnLocation;

 public int Health = 100;

 public int Ammo = 1; //This is the ammo in a single clip
 public int MaxAmmo = 3; //this variable will be adjusted for each gun ammount of ammo in each clip all together
 public float reloadAnimTime; //The actual animation time for each aim for a gun will be the float number
 public float reloadTime; //This number is the same as reloadanimtime but its there so you can set animtime back to its original numb

 void Awake () 
 {
     aimAnimInstance = this;
 }

 void Update ()
 {
     Aim ();
     Shoot ();
     Reload (); 
     Die ();
 }

 void Aim ()
 {
     if ((Input.GetMouseButtonDown(1)) && (IsAiming == false) && (isReloading == false)) 
     {
         GetComponentInChildren<Animation> ().Play ("Rocket_Aim");
         IsAiming = true;


     } 
     else if ((Input.GetMouseButtonDown(1)) && (IsAiming) && (isReloading == false)) 
     {
         GetComponentInChildren<Animation> ().Play ("Rocket_AimOut");
         IsAiming = false;
     }
 }

 void Shoot () 
 {
     if (Ammo > 0) {
         hasAmmo = true;
     } 
     else 
     {
         hasAmmo = false;
     }
     if (MaxAmmo > 0)
     {
         if ((Input.GetMouseButtonDown (0)) && (hasAmmo) && (isReloading == false)) 
         {
             Instantiate (bullet, spawnLocation.transform.position, spawnLocation.transform.rotation);
             Ammo--;
             MaxAmmo--;

             GetComponent<AudioSource>().PlayOneShot (shootSound, 1f);
             GetComponentInChildren<Animation> ().Play ("Rocket_Recoil");
         }
     }
 }

 void Reload () 
 {
     if (isReloading) 
     {
         reloadAnimTime -= 1 * Time.deltaTime;
         if (reloadAnimTime <= 0) 
         {
             isReloading = false;
             reloadAnimTime = reloadTime;
         }
     }

     if ((Ammo < 1) && (MaxAmmo > 0)) 
     {
         canReload = true;

         if ((Input.GetKeyDown (KeyCode.R)) && (canReload)) 
         {
             GetComponentInChildren<Animation> ().Play ("Rocket_Reload");
             IsAiming = false;
             isReloading = true;

             Ammo++;
         }
     }
 }

 void Die ()
 {
     if (Health <= 0) 
     {
         isDead = true;
         print ("Player died");
     }

     if (isDead) 
     {
         Application.LoadLevel ("Die_Scene");
     }
 }

 void OnTriggerEnter(Collider col) 
 {
     if (col.gameObject.tag == "R_Pickup") 
     {
         isPickedUp = true;

         if (isPickedUp) 
         {
             Destroy (gameObject, 0.7f);
             GetComponent<AudioSource> ().PlayOneShot (pickSound, 1f);
             AimAnim.aimAnimInstance.MaxAmmo++;
         } 
         else 
         {
             isPickedUp = false;
         }
     }
 }

}

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

  • Sort: 
avatar image
0
Best Answer

Answer by phxvyper · Mar 31, 2016 at 07:27 PM

In your OnTriggerEnter function you call Destroy(gameObject, 0.7f); which is the same thing as calling Destroy(this.gameObject, 0.7f);. You're telling Unity to Destroy the controller's game object, not the object tagged R_Pickup.

If you want to destroy the R_Pickup object, then do:

Destroy(other.transform.parent.gameObject, 0.7f);

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 Spider-og · Mar 31, 2016 at 07:30 PM 0
Share

Thank you so much! :)

Follow this Question

Answers Answers and Comments

43 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

Related Questions

destroy a non-trigger object hitting a trigger object? 1 Answer

Destroying Multiple Game Objects w/ OnTriggerEnter 1 Answer

How do I make this switch destroy an object? 1 Answer

OnTriggerEnter Influencing all GameObjects of the Same Type 2 Answers

Resources.load loads twice ( onTriggerenter->load-another->destroy-self ) 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