Search Unity

Question Prefab script member values not correct

Discussion in 'Prefabs' started by Horse_Manly, Aug 13, 2020.

  1. Horse_Manly

    Horse_Manly

    Joined:
    Mar 14, 2014
    Posts:
    3
    Tried posting this in unity answers but got "The form below has errors. Correct the fields in red and try again." Even though there are no red fields.

    In my scene I have a muzzle object parented to a gun, and the gun is parented to the player. As scene objects, everything was working fine, but when I converted the gun to a prefab the member variables become incorrect. The gun prefab is assigned to a PlayerScript.cs in the inspector and instantiated like this:
    Code (CSharp):
    1. Instantiate(m_shotgun, transform.position, transform.rotation);
    My input manager is then retrieving the gun prefab from the PlayerScript.cs then calling the ShootGun public function. How can this value be false?

    The gun prefab then has a script with `public bool m_canShoot = true;`. Even though it is never assigned to false and the inspector says it is true, the debugger has it as false. Weirdly enough this value is correct in the "Update" function but not in the ShootGun function.

    plz_help.png
     
  2. Horse_Manly

    Horse_Manly

    Joined:
    Mar 14, 2014
    Posts:
    3
    I must have some misunderstanding of how these member functions work. When I log to the console the position of the "m_muzzle" gameobject in the Update function, it changes as expected. But when I log it's position in the ShootGun() function, it remains the same always. Here is the full c# script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Threading;
    4. using UnityEngine;
    5.  
    6. public class GunScript : MonoBehaviour
    7. {
    8.     public string m_gunName;
    9.     public GameObject m_bullet;
    10.     public float m_fireRate;
    11.     public float m_damage;
    12.  
    13.     //private Animator m_anim;
    14.     public Animator m_muzzleAnim;
    15.     public GameObject m_muzzle;
    16.     public bool m_canShoot = true;
    17.     private float m_shootTimer;
    18.  
    19.     void Awake()
    20.     {
    21.         // always parent this to the player
    22.         transform.parent = GameObject.FindGameObjectWithTag("Player").transform;
    23.  
    24.         m_shootTimer = Time.time;
    25.     }
    26.  
    27.     private void Update()
    28.     {    
    29.         if (!m_canShoot)
    30.         {
    31.             // fire rate per second
    32.             float fireRateSec = 1f / m_fireRate;
    33.             if (Time.time - m_shootTimer >= fireRateSec)
    34.             {
    35.                 m_canShoot = true;
    36.                 m_shootTimer = Time.time;
    37.             }
    38.         }
    39.     }
    40.  
    41.     public void ShootGun()
    42.     {
    43.         Debug.Log(m_muzzle.transform.position);
    44.  
    45.         if (true)
    46.         {
    47.             if (m_muzzle != null) //muzzle flash and camerashake
    48.             {
    49.                 m_muzzleAnim.Play("Muzzle_" + m_gunName + "_Shoot");
    50.                 CameraShake.Shake(0.075f, 0.1f, 3f);
    51.             }
    52.             else {
    53.                 Debug.LogWarning("Muzzle not assigned in gun script.");
    54.             }
    55.  
    56.             if (m_bullet != null) //create bullet
    57.             {
    58.                 float angle = Common.PlayerMouseAngle();
    59.                 Instantiate(m_bullet, m_muzzle.transform.position, Quaternion.Euler(new Vector3(0f, 0f, angle)));
    60.             }
    61.             else {
    62.                 Debug.LogWarning("Bullet not assigned in gun script.");
    63.             }
    64.  
    65.             m_canShoot = false;
    66.         }
    67.     }
    68. }
    69.