Search Unity

Code works in editor, but after building it doesn't.

Discussion in 'Scripting' started by Creater2004, May 21, 2019.

  1. Creater2004

    Creater2004

    Joined:
    Mar 27, 2019
    Posts:
    5
    I have created a script for a homing missile that chases you and damages you once you get hit. It works fine in the editor, but after I build it, my character does not take any damage at all.
    (The missile is located in a prefab).

    This is my player script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour
    5. {
    6.  
    7.     //Floats
    8.     public float maxSpeed = 3;
    9.     public float speed = 50f;
    10.     public float jumpPower = 150f;
    11.  
    12.     //Booleans
    13.     public bool grounded;
    14.     public bool canDoubleJump;
    15.  
    16.     //Stats
    17.     public float curHealth;
    18.     public float max_health = 100f;
    19.  
    20.     //References
    21.     private Rigidbody2D rb2d;
    22.     private PlayerHealthSystem phs;
    23.     public Transform fp;
    24.  
    25.  
    26.  
    27.     void Start()
    28.     {
    29.         rb2d = gameObject.GetComponent<Rigidbody2D>();
    30.         phs = gameObject.GetComponent<PlayerHealthSystem>();
    31.         fp = this.gameObject.transform.GetChild(3);
    32.  
    33.         curHealth = max_health;
    34.  
    35.     }
    36.  
    37.  
    38.     void Update()
    39.     {
    40.  
    41.         if (Input.GetAxis("Horizontal") < -0.1f)
    42.         {
    43.             transform.localScale = new Vector3(-0.5f, 0.5f, 0.5f);
    44.  
    45.             Right();
    46.         }
    47.  
    48.         if (Input.GetAxis("Horizontal") > 0.1f)
    49.         {
    50.             transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    51.  
    52.             Left();
    53.         }
    54.  
    55.         if (Input.GetButtonDown("Jump"))        {
    56.             if (grounded)
    57.             {
    58.                 rb2d.AddForce(Vector2.up * jumpPower);
    59.                 canDoubleJump = true;
    60.             }
    61.             else
    62.             {
    63.  
    64.                 if (canDoubleJump)
    65.                 {
    66.  
    67.                     canDoubleJump = false;
    68.                     rb2d.velocity = new Vector2(rb2d.velocity.x, 0);
    69.                     rb2d.AddForce(Vector2.up * jumpPower / 1.75f);
    70.  
    71.                 }
    72.  
    73.             }
    74.         }
    75.  
    76.         if (curHealth > max_health)
    77.         {
    78.             curHealth = max_health;
    79.         }
    80.  
    81.         if (curHealth <= 0)
    82.         {
    83.  
    84.             curHealth = 0;
    85.  
    86.             Die();
    87.  
    88.         }
    89.  
    90.  
    91.  
    92.     }
    93.  
    94.     void FixedUpdate()
    95.     {
    96.         Vector3 easeVelocity = rb2d.velocity;
    97.         easeVelocity.y = rb2d.velocity.y;
    98.         easeVelocity.z = 0.0f;
    99.         easeVelocity.x *= 0.75f;
    100.  
    101.         float h = Input.GetAxis("Horizontal");
    102.  
    103.         /*//Fake friction / Easing the x speed of our player
    104.         if (grounded)
    105.         {
    106.  
    107.             rb2d.velocity = easeVelocity;
    108.  
    109.         }
    110.         */
    111.  
    112.         //Moving the player
    113.         rb2d.AddForce((Vector2.right * speed) * h);
    114.  
    115.         //Limiting the speed of the player
    116.         if (rb2d.velocity.x > maxSpeed)
    117.         {
    118.             rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
    119.         }
    120.  
    121.         if (rb2d.velocity.x < -maxSpeed)
    122.         {
    123.             rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
    124.         }
    125.        
    126.     }
    127.  
    128.     void Die()
    129.     {
    130.  
    131.         Application.LoadLevel(Application.loadedLevel);
    132.  
    133.     }
    134.  
    135.     public void Damage(int pain)
    136.     {
    137.  
    138.         curHealth -= pain;
    139.  
    140.     }
    141.  
    142.  
    143.     public IEnumerator Knockback(float knockDur, float knockbackPwr, Vector3 knockbackDir)
    144.     {
    145.  
    146.         float timer = 0;
    147.  
    148.         while (knockDur > timer)
    149.         {
    150.  
    151.             timer += Time.deltaTime;
    152.  
    153.             rb2d.AddForce(new Vector3(knockbackDir.x * -100, knockbackDir.y * knockbackPwr, transform.position.z));
    154.  
    155.         }
    156.  
    157.         yield return 0;
    158.  
    159.     }
    160.  
    161.     void Right()
    162.     {
    163.         fp.transform.localRotation = Quaternion.Euler(0, 0, 180f);
    164.     }
    165.  
    166.     void Left()
    167.     {
    168.         fp.transform.localRotation = Quaternion.Euler(0, 0, 0);
    169.     }
    170. }
    This is my missile script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Homing : MonoBehaviour
    5. {
    6.     private Player player;
    7.  
    8.     public float speed = 5;
    9.     public float rotatingSpeed = 200;
    10.     public int pain = 10;
    11.     public GameObject target;
    12.  
    13.     Rigidbody2D rb;
    14.  
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.  
    19.         target = GameObject.FindGameObjectWithTag("Play");
    20.         player = GameObject.FindGameObjectWithTag("Play").GetComponent<Player>();
    21.         rb = GetComponent<Rigidbody2D>();
    22.  
    23.  
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void FixedUpdate()
    28.     {
    29.  
    30.         Vector2 point2Target = (Vector2)transform.position - (Vector2)target.transform.position;
    31.  
    32.         point2Target.Normalize();
    33.  
    34.         float value = Vector3.Cross(point2Target, transform.right).z;
    35.  
    36.         /*
    37.         if (value > 0) {
    38.  
    39.                 rb.angularVelocity = rotatingSpeed;
    40.         } else if (value < 0)
    41.                 rb.angularVelocity = -rotatingSpeed;
    42.         else
    43.                 rotatingSpeed = 0;
    44. */
    45.  
    46.         rb.angularVelocity = rotatingSpeed * value;
    47.  
    48.  
    49.         rb.velocity = transform.right * speed;
    50.  
    51.  
    52.     }
    53.  
    54.  
    55.     public void OnTriggerEnter2D(Collider2D hitInfo)
    56.     {
    57.         if (hitInfo.tag == "Play")
    58.         {
    59.             player.Damage(pain);
    60.         }
    61.         Destroy(gameObject);
    62.     }
    63.  
    64.  
    65. }
    Could anyone help me? Thanks.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201