Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Object reference not set to an instance of an object (Please help)

Discussion in 'Scripting' started by briyan, Aug 9, 2015.

  1. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52
    Hello,

    I got a script were iam working on, and testing new things. But i tried to get acces to an script thats is in a gameobject so that i can change variables. The strange thing about this is, that i can change the variable's but also get a error. Normaly when a line contains errors it's not working, but in this case it's stil working o_O. I hope someone can see what's wrong with my code.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Soldier1_Swing_Sword_Test : MonoBehaviour {
    5.  
    6.     public RaycastHit hit;
    7.     public int damage;
    8.     public bool hite;
    9.     public float Old_Y;
    10.     public static bool Taken_Damage;
    11.     public static bool good;
    12.     public GameObject soldier1;
    13.     public Soldier1_AI_Test other;
    14.     public bool test;
    15.     // Use this for initialization
    16.     void Start () {
    17.         Taken_Damage = false;
    18.         damage = 20;
    19.         Old_Y = 0.0f;
    20.  
    21.         other = (Soldier1_AI_Test) soldier1.GetComponent(typeof(Soldier1_AI_Test));
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.  
    27. //this line gives a error    
    28. test = other.Is_Swinging;
    29.         if(test == true && hite == false){
    30.             if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.up), out hit)){
    31.                 float distance = hit.distance;
    32.                 if(hit.transform.tag == "Enemy"){
    33.                     Debug.Log ("Enemy has been hit");
    34.                     hite = true;
    35.                     good = true;
    36.                     hit.transform.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
    37.                 }
    38.  
    39.                 Old_Y = hit.transform.position.y;
    40.                 StartCoroutine(Damage_Timer());
    41.             }
    42.            
    43.         }
    44.         if(good == true){
    45.  
    46.         }
    47.     }
    48.    
    49.     IEnumerator Damage_Timer(){
    50.         yield return new WaitForSeconds(2.0f);
    51.  
    52.     }
    53.  
    54. }
    55.  
     
  2. artaka

    artaka

    Joined:
    Feb 19, 2013
    Posts:
    128
    try changing line 21 to:
    Code (CSharp):
    1. other = soldier1.GetComponent<Soldier1_AI_Test>();
    and just double check that soldier1 contains a Soldier1_AI_Test component.
     
    Polymorphik likes this.
  3. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Code (csharp):
    1. if(test == true && hite ==false){
    to
    Code (csharp):
    1. if(other && other.Is_Swinging && !hite){
    But yes, it seems like there's no component of that type attached to that soldier1 GameObject, so it's returning null and you're not null-checking it.
     
  4. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52
    Wel, the soldier contains a soldier1 ai, and i the game the code is doing what its suposed to do, but with errors
     
  5. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52
    iam such a fool........

    I cloned the soldier to test the combat and only deleted the soldier ai in that clone and forgot to delete the soldier swing test script..... the reason why i deleted the AI in the cloned soldier was because i changed the tag to Enemy so that the cloned soldier would be attacked and didn't attack himself... sorry for opening this thread because of such a dumb mistake hahah.
     
    sterynkng14 likes this.