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

Inspector not interacting with Script correctly

Discussion in 'Scripting' started by Xnanga, Nov 28, 2012.

  1. Xnanga

    Xnanga

    Joined:
    Nov 28, 2012
    Posts:
    7
    Hello everyone

    I've only been introduced to Unity and scripting for a few weeks now so I'm sorry if this is a really stupid question :p

    I have a script which I've attached to the player for multiple functions such as audio, raycasting etc.

    My problem is that when I declare a public gameObject, audioclip or Animation, the Unity Inspector won't let me choose what the object is from the scene hierarchy. Or sometimes it will show me an empty assets list.

    I'm thinking there might be something wrong with my script as all my other scripts seem to be working fine, anyone notice anything that may be the problem?


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerScript : MonoBehaviour {
    5.    
    6.     //Prefab. add more prefabs
    7.     public GameObject LongBow;
    8.    
    9.     //GameObject
    10.     public GameObject WeaponProp;
    11.    
    12.     //Current Weapon
    13.     public GameObject weapon;
    14.    
    15.     //Audio
    16.     public AudioClip footStepSound;
    17.     public AudioClip jumpSound;
    18.    
    19.     //Integers
    20.     public int score=0;
    21.     public int MaxHealth=350;
    22.     public float Health;
    23.    
    24.     //Animations
    25.     private Animation EnemyDeath;
    26.    
    27.     //GUI Crosshair
    28.     private Vector3 screenCentre;
    29.    
    30.     // Use this for initialization
    31.     void Start () {
    32.        
    33.         screenCentre=new Vector3(Screen.width/2,
    34.             Screen.height/2,0);
    35.        
    36.         Health = MaxHealth;
    37.  
    38.     }
    39.    
    40.     // Update is called once per frame
    41.     void Update () {
    42.         if (Input.GetButtonDown("Horizontal") ||Input.GetButtonDown("Vertical"))
    43.         {
    44.             if (!audio.isPlaying)
    45.             {
    46.                 audio.loop=true;
    47.                 audio.PlayOneShot(footStepSound);
    48.             }
    49.         }
    50.         else if (Input.GetButtonUp("Horizontal") || Input.GetButtonUp("Vertical"))
    51.         {
    52.             audio.loop=false;
    53.             audio.Stop();
    54.         }
    55.        
    56.         if (Input.GetButtonDown("Jump"))
    57.         {
    58.             audio.PlayOneShot(jumpSound);
    59.         }
    60.         if (weapon!=null){
    61.             if (weapon.active)
    62.             {
    63.                 if (Input.GetButtonDown("Fire1"))
    64.                 {
    65.                     weapon.audio.Play();
    66.                     Ray ray=Camera.main.ScreenPointToRay(screenCentre);
    67.                     RaycastHit hit=new RaycastHit();
    68.                     if (Physics.Raycast(ray,out hit))
    69.                     {
    70.                         if (hit.collider.gameObject.tag=="Enemy")
    71.                         {
    72.                             Debug.Log("Hit Monster");
    73.                             TargetScript targetScript=
    74.                                         hit.collider.gameObject.
    75.                                         GetComponent<TargetScript>();
    76.                             if (targetScript!=null)
    77.                             {
    78.                                 targetScript.Hit();
    79.                                
    80.                             }
    81.                             score++;                
    82.                         }
    83.                     }
    84.                 }
    85.             }
    86.         }
    87.     }
    88.    
    89.     void OnControllerColliderHit(ControllerColliderHit hit)
    90.     {
    91.        
    92.         if (hit.gameObject.tag=="Pickups")
    93.         {
    94.             if (hit.gameObject.name=="Longbow")
    95.             {
    96.                 weapon=(GameObject)Instantiate(LongBow, WeaponProp.transform.position,
    97.                 Quaternion.identity);
    98.                 weapon.transform.parent=WeaponProp.transform;
    99.             }
    100.             Debug.Log("Object picked up"+
    101.                                          hit.gameObject.name);
    102.             Destroy(hit.gameObject);
    103.            
    104.         }
    105.         if (hit.gameObject.name=="NextLevelTrigger")
    106.             {
    107.                 Application.LoadLevel("Outside Path");
    108.                 Debug.Log("Loading Next Level");
    109.         }
    110.         if (hit.gameObject.tag=="Enemy")
    111.         {
    112.             Health--;
    113.         }
    114.     }
    115.            
    116.        
    117.        
    118.    
    119.  
    120.     void OnGUI()
    121.     {
    122.         GameObject gameData=GameObject.Find("GameData");
    123.         if (gameData!=null)
    124.         {
    125.             GameDataScript gameDataScript=gameData.GetComponent<GameDataScript>();
    126.             GUI.Label(new Rect(0,10,100,100),gameDataScript.playerName+" Score: "+score.ToString());
    127.         }
    128.        
    129.     }
    130.  
    131. }