- Home /
problem spawning bullets in c#
i am currently working on the controls for my game. they will be similar to robotron,smash tv, or geometry. i have been trying to figure out for 2 days now how to instantiate my projectile prefab at the correct rotation and angle to match my right thumbstick input.
The prefab i am trying to spawn is placed here Assets/Prefabs/Projectile.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
Vector3 vNewInput = new Vector3(Input.GetAxis("Right Horizontal"), Input.GetAxis("Right Vertical"), 0.0f);
if (vNewInput.sqrMagnitude < 0.1f)
{
return;
}
else
{
var direction = new Vector3 (Input.GetAxis ("Right Horizontal"), Input.GetAxis ("Right Vertical"), 0.0f);
var rotation = Quaternion.LookRotation (direction, Vector3.up);
transform.rotation = rotation;
Instantiate (Projectile, direction, rotation);
What errors do you get? What's the effect of your pasted code?
Answer by retrohead · Oct 02, 2014 at 12:41 AM
Here's the full code and errors I'm getting
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
public Rigidbody projectile;
public float speed = 0.2f;
Animator animator;
int dirInt;
Vector3 v3;
Vector3 shoot;
// Use this for initialization
void Start () {
animator = this.GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
float vertical = Input.GetAxis ("Vertical");
float horizontal = Input.GetAxis ("Horizontal");
//This moves our player
v3 = new Vector3(Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical"),0);
transform.Translate(speed * v3.normalized);
//This Lets our player shoot
Vector3 shootangle = new Vector3(Input.GetAxis("Right Horizontal"), Input.GetAxis("Right Vertical"), 0f);
//Only do work if meaningful
if (shootangle.sqrMagnitude < 0.1f)
{
return;
}
else
{
var direction = new Vector3 (Input.GetAxis ("Right Horizontal"), Input.GetAxis ("Right Vertical"), 0.0f);
var rotation = Quaternion.LookRotation (direction, Vector3.up);
transform.rotation = rotation;
Instantiate (Projectile , direction, rotation);
}
//This controls the direction we face and which animation plays
if (horizontal > 0) {
animator.SetInteger ("Direction", 1);
dirInt = 1;
} else if (horizontal < 0) {
animator.SetInteger ("Direction", 3);
dirInt = 3;
} else if (vertical > 0) {
animator.SetInteger ("Direction", 2);
dirInt = 2;
} else if (vertical < 0) {
animator.SetInteger ("Direction", 0);
dirInt = 0;
} else if (horizontal > 0 && vertical > 0) {
animator.SetInteger ("Direction", 1);
dirInt = 1;
}
else if(horizontal == 0)
{
if(dirInt == 1)
{
animator.SetInteger("Direction",5);
}
if(dirInt == 2)
{
animator.SetInteger("Direction",6);
}
if(dirInt == 3)
{
animator.SetInteger("Direction",7);
}
if(dirInt == 0)
{
animator.SetInteger("Direction",4);
}
}
}
}
Assets/Scripts/Player.cs(52,62): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected
Assets/Scripts/Player.cs(52,49): error CS1502: The best overloaded method match for UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments Assets/Scripts/Player.cs(52,49): error CS1503: Argument
#1' cannot convert object' expression to type
UnityEngine.Object'
You have a script named Projectile right? C# is case sensitive. In other words, 'P' should be 'p'
Answer by OboShape · Oct 02, 2014 at 10:19 AM
you have declared projectile on Line 6
public Rigidbody projectile;
however on line 52 you are instantiating Projectile prefab.
Instantiate (Projectile , direction, rotation);
as revolute says C# is case sensitive.
ensure that all entries to your projectile are of the same case. amment line 52 to read..
Instantiate (projectile , direction, rotation);
See how that goes :)
thank your all for you replys, after changing Projectile to projectile my error now comes while the game is running and i atempt to shoot. i get this error...
UnassignedReferenceException: The variable projectile of Player has not been assigned. You probably need to assign the projectile variable of the Player script in the inspector. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:74) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:84) Player.Update () (at Assets/Scripts/Player.cs:52)
it appears to be looking on my player game object for the rigidbody variable, which is the gameobject with the above script attatched. i am unsure how to reference my prefab ins$$anonymous$$d from the player script
never$$anonymous$$d, i understand now. this absolutely fixed my problem. it made a variable in the inspector on my player gameobject i could attach my prefab too. thank you very much for your help.
Your answer
![](https://koobas.hobune.stream/wayback/20220613164208im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate prefab (button, texture) with attached script 1 Answer
Having multiple objects fire prefabs in different times C# 0 Answers
Object after instantiate doesn't apply AddForce 1 Answer
instantiate prefab assigned in inspector c# 1 Answer
Clones are still in the hierarchy after exiting play mode 2 Answers