Click here to Skip to main content
15,886,019 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
I have an enemy that references a hero script!

a hero that references an enemy script!

towers that do damage to the enemy!

(All as objects the game works)

when I turn the enemy into a prefab and the hero and spawn enemies all the referenced scripts targets break, I relink them all in the prefabs and update

but then nothing works! the enemy doesn't take damage, ignores the hero!

What am I doing wrong :(

What I have tried:

Ive tried for days and searched the internet for answers!

Here is the enemy Code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine.Unity;
using Spine;
using UnityEngine.UI;
 
public class Enemy : MonoBehaviour
       {
    private bool Electrickcuted;

    private bool WaterBombed;

    private float electrickcuted = 2f;

    private float HitByWater = 2f;
   
    public EnemyMove moveScript;

    public Police PoliceScript;

    public SkeletonAnimation SkelAnim;

    public float EnemyStartHealth =100;

    public Image EnemyHealthBar;

    public float Enemyhealth;

    public GameObject HealthContainer;

    [SpineAnimation]
    public string walk;

    [SpineAnimation]
    public string electrick;
   
    [SpineAnimation]
    public string attack;
 
   [SpineAnimation]
    public string stand;

    [SpineAnimation]
    public string die;

    [SpineAnimation]
    public string water;

   


    void Awake(){

    SkelAnim = GetComponent<SkeletonAnimation>();

    SkelAnim.state.SetAnimation(0,walk,true); 

    
      
        }

    void Start()

       {  
    
    Enemyhealth = EnemyStartHealth;
    
    SkelAnim.state.SetAnimation(0,walk,true);

    System.Random randomNumberGenerator = new System.Random();

    int randomSkinNumber = randomNumberGenerator.Next(0, 60); 

    string skinName = "football_" + randomSkinNumber; 

    SkelAnim.skeleton.SetSkin(skinName ); 

       }

    public void TakeDamgeEnemy (float amount)
    
       {

    Enemyhealth -= amount;

    EnemyHealthBar.fillAmount =  Enemyhealth/EnemyStartHealth;

    if (Enemyhealth <=0)
        {
    Destroy(GameObject.Find("TakeDamageEnemy"));
    Destroy(GameObject.Find("HealthContainerEnemy"));  
    Destroy(GameObject.Find("enemyweapon")); 
    Destroy(GameObject.Find("EnemyFoot")); 

    moveScript.WalkSpeed = 0f;

    WaterBombed = false;

    Electrickcuted = false;

    SkelAnim.state.SetAnimation(0,die,false);
       
        }
   

        }

    private void Update()
      {
      if(Electrickcuted)
      {
      ElectrickAttack();
      }
      if(WaterBombed)
      {
      WaterAttack();
      }
      
      }



     void OnTriggerEnter2D (Collider2D other)            
      
     {


    if (other.gameObject.CompareTag("policeFootCol")) 
     {
    SkelAnim.state.SetAnimation(0,attack,true); 

     moveScript.WalkSpeed = 0f;
 
    TakeDamgeEnemy (0f);
     }

    if (other.gameObject.CompareTag("PoliceWeapon"))  
     {
     TakeDamgeEnemy (10f);
     }


    if  (PoliceScript.policehealth <=0)
     {
  
    PoliceScript.policehealth = 0; 
   
    SkelAnim.state.SetAnimation(0,walk,true);

    moveScript.WalkSpeed = 1f;
      }


    if (other.gameObject.CompareTag("bullet")) 

      {
      Electrickcuted = true;

      SkelAnim.state.SetAnimation(0,electrick,true);

      moveScript.WalkSpeed = 0f;

      TakeDamgeEnemy (15f);
      }


    if (other.gameObject.CompareTag("WaterBomb")) 
      {

    WaterBombed = true;

    SkelAnim.state.SetAnimation(0,water,false);
    
    moveScript.WalkSpeed = 0f;

    TakeDamgeEnemy (20f);
      }

      }
     
    private void ElectrickAttack()
     {

    if(electrickcuted < 0f)
     
     {

    Electrickcuted = false;

      SkelAnim.state.SetAnimation(0,walk,true);
     
      moveScript.WalkSpeed = 1f;   

      electrickcuted = 2f;

      }else{

      electrickcuted -= Time.deltaTime;


        }
        }

    private void WaterAttack()
      {
      if(HitByWater< 0f)
      {

      WaterBombed = false;

      SkelAnim.state.SetAnimation(0,walk,true);
     
      moveScript.WalkSpeed = 1f;  

      HitByWater = 2f; 

      

      }else{

      HitByWater -= Time.deltaTime;

     }

  }

}
Posted
Updated 1-Feb-19 3:36am
v3
Comments
CHill60 1-Feb-19 8:12am    
Nowhere near enough information to help really. Besides, you should really approach the Unity community for help with this - Community - Unity[^]
Member 14135033 1-Feb-19 9:07am    
ok I will post there also
Dave Kreskowiak 1-Feb-19 8:21am    
Wrong? Probably everything, maybe nothing. We can't tell because we can't see any of your code at all.
Member 14135033 1-Feb-19 9:06am    
added enemy script, all works as an object
Dave Kreskowiak 1-Feb-19 9:19am    
You've already been told this, but your code is STILL so badly formatted as to be unreadable.

Start by cleaning up the placement of all the curly braces and proper indentation. That alone makes it far easier to debug the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900