Search Unity

Enemy AI script Object reference instance error?

Discussion in 'Getting Started' started by Aardwing, Nov 4, 2015.

  1. Aardwing

    Aardwing

    Joined:
    Nov 3, 2015
    Posts:
    24
    This is my first ever script I made for AI in unity.What im trying to accomplish is the AI (a cube) to shoot when he is at a distance of the player,but as soon as the AI enters the shooting range it throws : "Object reference not set to an instance of an object". Here is my code:


    Code (JavaScript):
    1. var Player : Transform;
    2. var MoveSpeed = 4;
    3. var MaxDist = 0;
    4. var MinDist = 0;
    5. var enemybullet :GameObject;
    6. var barrelEnd :Transform;
    7. function Shoot(){
    8. var enemybulletInstance : Rigidbody;
    9.        enemybullettInstance = Instantiate(enemybullet, barrelEnd.position, barrelEnd.rotation);
    10.      enemybulletInstance.AddForce(barrelEnd.forward * 5000);
    11.      }
    12. function Start ()
    13. {
    14. }
    15. function Update ()
    16. {
    17.      transform.LookAt(Player);
    18.    
    19.      if(Vector3.Distance(transform.position,Player.position) >= MinDist){
    20.    
    21.           transform.position += transform.forward*MoveSpeed*Time.deltaTime;
    22.          
    23.          
    24.           if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
    25.               {
    26.                  Shoot();
    27.     }
    28.    
    29.     }
    30. }
    I do not have any idea what can cause this since I did everything correctly as far as I know.Any help is appreciated.
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    What line is the error? At a guess it probably doesn't know what the enemy bullet is so it can use the prefab.
     
  3. Aardwing

    Aardwing

    Joined:
    Nov 3, 2015
    Posts:
    24
    Taking a closer look at the error at line 11 I noticed that line 9 "enemybullett" is mispelled.Now,I am getting an error at line 10 saying "Cannot cast from source type to destination type".
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    If you are still having issues, can you post your current code?

    Also, when posting code, can you make sure that the indents are properly aligned and that any unused code is removed?

    This is the code you posted above:
    Code (JavaScript):
    1. var Player : Transform;
    2. var MoveSpeed = 4;
    3. var MaxDist = 0;
    4. var MinDist = 0;
    5. var enemybullet :GameObject;
    6. var barrelEnd :Transform;
    7. function Shoot(){
    8. var enemybulletInstance : Rigidbody;
    9.        enemybullettInstance = Instantiate(enemybullet, barrelEnd.position, barrelEnd.rotation);
    10.      enemybulletInstance.AddForce(barrelEnd.forward * 5000);
    11.      }
    12. function Start ()
    13. {
    14. }
    15. function Update ()
    16. {
    17.      transform.LookAt(Player);
    18.    
    19.      if(Vector3.Distance(transform.position,Player.position) >= MinDist){
    20.    
    21.           transform.position += transform.forward*MoveSpeed*Time.deltaTime;
    22.          
    23.          
    24.           if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
    25.               {
    26.                  Shoot();
    27.     }
    28.  
    29.     }
    30. }
    Now, when it's "clean", isn't this more readable?

    Code (JavaScript):
    1. var player : Transform;
    2. var moveSpeed = 4;
    3. var maxDist = 0;
    4. var minDist = 0;
    5. var enemybullet : GameObject;
    6. var barrelEnd : Transform;
    7.  
    8. function Update ()
    9. {
    10.      transform.LookAt (player);
    11.      if (Vector3.Distance (transform.position, player.position) >= minDist)
    12.      {  
    13.           transform.position += transform.forward * moveSpeed * Time.deltaTime;
    14.           if (Vector3.Distance (transform.position, player.position) <= maxDist)
    15.           {
    16.                Shoot ();
    17.           }  
    18.      }
    19. }
    20.  
    21. function Shoot ()
    22. {
    23.      var enemybulletInstance : Rigidbody;
    24.      enemybullettInstance = Instantiate (enemybullet, barrelEnd.position, barrelEnd.rotation);
    25.      enemybulletInstance.AddForce (barrelEnd.forward * 5000);
    26. }
    Not only did I align the indents, but I also made consistent spaces and brace styles in the code. Lastly, I also made the style of the variables consistent. Some started with a leading capital letter, and others did not. With Unity, variables generally start with a lowerCaseLetter, and Types and Functions with an UpperCaseLetter

    It doesn't matter if the braces are in-line or dropped, and it doesn't matter if there is a space between the function name and the opening parentheses - but they should be consistent. The original code mixed these throughout the block.
     
  5. Aardwing

    Aardwing

    Joined:
    Nov 3, 2015
    Posts:
    24
    I managed to get my code fixed,and yes,I know it is alot better if my code is clean.Here it is the code,but it still needs some adjustments:

    Code (JavaScript):
    1. var Player : Transform;
    2. var MoveSpeed = 4;
    3. var MaxDist = 0;
    4. var MinDist = 0;
    5. var enemybullet : Rigidbody;
    6. var barrelEnd :Transform;
    7. function Shoot(){
    8. var enemybulletInstance :  Rigidbody;;
    9.      enemybulletInstance = Instantiate(enemybullet, barrelEnd.position, barrelEnd.rotation);
    10.      enemybulletInstance.AddForce(barrelEnd.forward * 5000);
    11.      }
    12. function Start ()
    13. {
    14. }
    15. function Update ()
    16. {
    17.      transform.LookAt(Player);
    18.    
    19.      if(Vector3.Distance(transform.position,Player.position) >= MinDist){
    20.    
    21.           transform.position += transform.forward*MoveSpeed*Time.deltaTime;
    22.          
    23.          
    24.           if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
    25.               {
    26.               Shoot(); //Shoot or something
    27.     }
    28.  
    29.     }
    30. }
    31.