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 /
avatar image
0
Question by MilgasiKrook · Sep 16, 2015 at 11:44 PM · instantiatetriggerdamage

Dealing damage problem

I have 3 scripts that is supposed to deal damage to my enemy (for melee combat). And also both the player and the enemy has the "Stats" script attached to them because i want this to be multiplayer at a later stage. This is the first script:

 using UnityEngine;
 using System.Collections;
 
 public class CharacterInputdenandra : MonoBehaviour
 {
 
 void FixedUpdate () {
 ControllPlayer()
 }
 
 void ControllPlayer () {
 if (Input.GetButtonDown ("Fire1")) {
 
 GameObject go = Instantiate(Resources.Load("GS_Hitbox"), transform.position+(transform.forward*2)+(transform.up*2)+(transform.right*-1.5f) , transform.rotation) as GameObject;
 go.transform.parent = transform;
 Gs_HITDETECT = go;
 Invoke ("Amove",0.4f);
 }
 }
 
 void Amove () {
 DestroyObject (Gs_HITDETECT);
 }
 }

This is used to create the prefab which is a trigger that detects if any enemy is inside. The prefab has this script attached to it:

 using UnityEngine;
 using System.Collections;
 
 public class HitDetection : MonoBehaviour {
     public GameObject enemy;
     public int Weapondamage;
     Stats enemyhealth;
     
     void Awake () {
     }
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         Weapondamage = transform.root.gameObject.GetComponent<Stats> ().damage;
     }
     
     
     void OnTriggerEnter(Collider other) {
         if (other.tag == "Enemy") {
             enemy = other.gameObject;
             enemyhealth = enemy.GetComponent<Stats>();
             enemyhealth.TakeDamage (Weapondamage);
             print ("hit");
         }
     }
 }

Until here evrything works fine. if the enemy is hit i get the print right. The problem is when i acctually want to deal the damage in the Stats script attached to the enemy.

 using UnityEngine;
 using System.Collections;
 
 public class Stats : MonoBehaviour {
     public int maxhealth = 100;
     public int curhealth = 100;
     public int damage = 12;
     public bool hascollide = false;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (curhealth <= 0f) {
             Destroy(gameObject);
         }
     
     }
 
     public void TakeDamage (int amount) {
         curhealth -= amount;
 
     }

}

The wierdest thing is that if i run the game as it is i cant deal any damage to the enemy, but if i go back to the first script and remove the part where i destroy the trigger, i can move around so just one "GS_Hitbox" can hit the enemy more than once, the enemy takes damage.

So to sum this up the problem is that my trigger does not deal damage when instantiated. Can someone please help me with this issue?

Comment
Add comment · Show 2
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 Rostam24 · Sep 17, 2015 at 07:45 AM 0
Share

I don't see any obvious problems in your code on my mobile phone. I did notice you are using a lot of public variables though. Any chance you've changed them over time...? If so, you might want to check the editor to see if the editor is still enforcing the old values (as public variables can be set from the editor).

avatar image MilgasiKrook · Sep 21, 2015 at 09:38 PM 0
Share

well the public variables is not the problem since it works when its not instantiated, i just need to know why it doesn't work when the trigger is instantiated :S

0 Replies

· Add your reply
  • Sort: 

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

30 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

Related Questions

Apply more damage over time?,How can I apply more damage over time? 0 Answers

Destroy instatiate object on trigger enter / collision,destroy instantiate prefab on trigger enter 0 Answers

i got two rotating objects that hit each other so i want to instantiate sparks at colliding point. (oncollision) and (ontrigger) 1 Answer

Help spawning particle systems on collision enter 0 Answers

How To Instantiate On Acceleration 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