Search Unity

Error Shooting entity

Discussion in 'Scripting' started by BasdeWildt, May 4, 2018.

  1. BasdeWildt

    BasdeWildt

    Joined:
    May 4, 2018
    Posts:
    13
    I am trying to get the game to shoot a projectile from the player. it is under addets, recources and its called projectile. when klicking to shoot i get this error:
    ArgumentException: The Object you want to instantiate is null.
    UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:353)
    UnityEngine.Object.Instantiate[GameObject] (UnityEngine.GameObject original) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:251)
    launch.Update () (at Assets/launch.cs:17)


    Here is my script
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class launch : MonoBehaviour {
    5.  
    6.     GameObject prefab;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         prefab = Resources.Load("projectile") as GameObject;
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.     if (Input.GetMouseButtonDown(0))
    16.         {
    17.             GameObject projectile = Instantiate(prefab) as GameObject;
    18.             projectile.transform.position = transform.position + Camera.main.transform.forward * 2;
    19.             Rigidbody rb = projectile.GetComponent<Rigidbody>();
    20.             rb.velocity = Camera.main.transform.forward * 40f;
    21.         }
    22.     }
    23. }
    24.  
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Make sure the folder is "Resources" and that "projectile" is spelled and capitalized correctly to match the asset.
     
  3. BasdeWildt

    BasdeWildt

    Joined:
    May 4, 2018
    Posts:
    13
    It is,
     

    Attached Files:

  4. BasdeWildt

    BasdeWildt

    Joined:
    May 4, 2018
    Posts:
    13
    oh wait
    it is reCources
     
  5. BasdeWildt

    BasdeWildt

    Joined:
    May 4, 2018
    Posts:
    13
    now it sais Assets/launch.cs(10,18): error CS0103: The name `Recources' does not exist in the current context
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I was talking about the folder.

    That new error is because you misspelled "Resources".
     
  7. BasdeWildt

    BasdeWildt

    Joined:
    May 4, 2018
    Posts:
    13
    yes and i re
    The folder is called recources, in my script it now also sais recources and now the error is Assets/launch.cs(10,18): error CS0103: The name `Recources' does not exist in the current context
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sorry, no .. that is backwards. You must name the folder "Resources" and change the code to that, too. :)
     
  9. BasdeWildt

    BasdeWildt

    Joined:
    May 4, 2018
    Posts:
    13
    Thank you, it works. could you help me with 1 more thing?

    I have a script for movement and mousecontroll, but the mouse movement on the y axis doesnt work, but iit doesnt give any errors. Could you take a look?
    My script is:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class FPSController : MonoBehaviour {
    5.  
    6.     CharacterController player;
    7.  
    8.     public float speed = 4f;
    9.     public float sensitivity = 4f;
    10.  
    11.     float moveFB;
    12.     float moveLR;
    13.  
    14.     float rotX;
    15.     float rotY;
    16. }
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.  
    21.         Cursor.lockState = CursorLockMode.Locked;
    22.         player = GetComponent<CharacterController>();
    23.  
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update () {
    28.  
    29.         moveFB = Input.GetAxisRaw("Vertical") * speed;
    30.         moveLR = Input.GetAxisRaw("Horizontal") * speed;
    31.  
    32.         rotX = Input.GetAxis("Mouse X") * sensitivity;
    33.         RotY = Input.GetAxis("Mouse Y") * sensitivity;
    34.  
    35.         Vector3 movement = new Vector3(moveLR, 0, moveFB);
    36.         transform.Rotate(0, rotX, 0);
    37.  
    38.         movement = transform.rotation * movement;
    39.         player.Move(motion: movement * Time.deltaTime);
    40.  
    41.         if (Input.GetKeyDown("escape"))
    42.             Cursor.lockState = CursorLockMode.None;
    43.  
    44.     }
    45. }
    46.  
     
  10. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're not actually using RotY once it's set...
     
  11. BasdeWildt

    BasdeWildt

    Joined:
    May 4, 2018
    Posts:
    13
    How do i fix it than
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Do you mean the rotation on the Y axis isn't doing anything?

    I copied and pasted your code. You had 2 errors, line #s : 16 and 33. Delete line #16 = ' }' and line 33 should be rotY =
    I tried your code and it's working well.

    As noted, you weren't actually using rotY, but other than that it was working ;)
     
  13. BasdeWildt

    BasdeWildt

    Joined:
    May 4, 2018
    Posts:
    13
    i changed and now it says Assets/FPSController.cs(15,11): warning CS0414: The private field `FPSController.rotY' is assigned but its value is never used
    Assets/FPSController.cs(15,11): warning CS0414: The private field `FPSController.rotY' is assigned but its value is never used