Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Error compiling the proyect

Discussion in 'Scripting' started by Nixel2013, Aug 10, 2019.

  1. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    The story is the following:

    I have a project in which there are "traps" that hurt, but when building the project those "traps" don't hurt, the enemies don't hurt either.

    Can you know why?
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Not unless you post your scripts and/or any error messages you're getting.
     
  3. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    That is the problem, no error jumps or occurs
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Can you please post your scripts?
     
  5. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class DañoPinchosSimples : MonoBehaviour
    7. {
    8.  
    9.     public float timeBetweenAttacks = 0.5f;
    10.     public int attackDamage = 1;
    11.  
    12.     GameObject player;
    13.     Health2 health2;
    14.     VidaEnemigo enemyHealth;
    15.  
    16.     bool PlayerInRange;
    17.     float timer;
    18.  
    19.  
    20.     void Awake()
    21.     {
    22.         player = GameObject.FindGameObjectWithTag("Player");
    23.         health2 = player.GetComponent<Health2>();
    24.         enemyHealth = GetComponent<VidaEnemigo>();
    25.  
    26.     }
    27.  
    28.     void OnTriggerEnter(Collider other)
    29.     {
    30.         if (other.gameObject == player)
    31.         {
    32.             PlayerInRange = true;
    33.  
    34.         }
    35.     }
    36.  
    37.     void OnTriggerExit(Collider other)
    38.     {
    39.         if (other.gameObject == player)
    40.         {
    41.             PlayerInRange = false;
    42.  
    43.         }
    44.  
    45.     }
    46.  
    47.  
    48.     // Start is called before the first frame update
    49.     void Start()
    50.     {
    51.  
    52.     }
    53.  
    54.     // Update is called once per frame
    55.     void Update()
    56.     {
    57.         timer += Time.deltaTime;
    58.  
    59.         if (timer >= timeBetweenAttacks && PlayerInRange && enemyHealth.currentHealth > 0)
    60.         {
    61.             Attack();
    62.         }
    63.  
    64.        
    65.  
    66.     }
    67.  
    68.     void Attack()
    69.     {
    70.         timer = 0f;
    71.  
    72.         if (health2.currenthealth > 0)
    73.         {
    74.             health2.TakeDamage(attackDamage);
    75.  
    76.         }
    77.  
    78.     }
    79.  
    80.    
    81.  
    82. }
     
  6. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    Puedo infligir daño a los objetos, pero los objetos no pueden dañarme, por lo que no entiendo el problema
     
  7. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Your script seems like it should be working fine.
    Try entering debug mode or inserting some Debug.Logs in certain places to check if the values of your fields are what they should be.

    For instance, perhaps you have more than one GameObject in your scene tagged as "Player", and this line is returning the object that you weren't expecting:
    Code (CSharp):
    1. player = GameObject.FindGameObjectWithTag("Player");
    Or maybe your
    Health2
    script's
    TakeDamage()
    method isn't functioning as intended.
     
    Nixel2013 likes this.
  8. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    as I say, in the Unity project everything works perfectly, but when building the project, EVERYTHING works, except that the enemies inflict damage on me