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): using System.Collections; using UnityEngine; public class launch : MonoBehaviour { GameObject prefab; // Use this for initialization void Start () { prefab = Resources.Load("projectile") as GameObject; } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0)) { GameObject projectile = Instantiate(prefab) as GameObject; projectile.transform.position = transform.position + Camera.main.transform.forward * 2; Rigidbody rb = projectile.GetComponent<Rigidbody>(); rb.velocity = Camera.main.transform.forward * 40f; } } }
Make sure the folder is "Resources" and that "projectile" is spelled and capitalized correctly to match the asset.
now it sais Assets/launch.cs(10,18): error CS0103: The name `Recources' does not exist in the current context
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
Sorry, no .. that is backwards. You must name the folder "Resources" and change the code to that, too.
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): using System.Collections; using UnityEngine; public class FPSController : MonoBehaviour { CharacterController player; public float speed = 4f; public float sensitivity = 4f; float moveFB; float moveLR; float rotX; float rotY; } // Use this for initialization void Start () { Cursor.lockState = CursorLockMode.Locked; player = GetComponent<CharacterController>(); } // Update is called once per frame void Update () { moveFB = Input.GetAxisRaw("Vertical") * speed; moveLR = Input.GetAxisRaw("Horizontal") * speed; rotX = Input.GetAxis("Mouse X") * sensitivity; RotY = Input.GetAxis("Mouse Y") * sensitivity; Vector3 movement = new Vector3(moveLR, 0, moveFB); transform.Rotate(0, rotX, 0); movement = transform.rotation * movement; player.Move(motion: movement * Time.deltaTime); if (Input.GetKeyDown("escape")) Cursor.lockState = CursorLockMode.None; } }
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
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