This is my code, it has a problem with the line that says:
Rigidbody instantiatedProjectile = Instantiate (projectile, transform.position, transform.rotation) as Rigidbody;
it keeps saying "Embedded statement cannot be a declaration or labeled statement"
I am trying to make an empty object shoot a tank shell and this class is called FireShell.cs
I am trying to make it shoot when I press space, and I can’t bind space to Fire1. Either way the error won’t let me run it in unity.
using UnityEngine;
using System.Collections;
public class FireShell : MonoBehaviour {
public Rigidbody projectile;
public float speed = 50;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1"))
{
Rigidbody instantiatedProjectile = Instantiate (projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection (new Vector3 (0, 0, speed));
}
}
}
Hi guys. I just tried to fix my code and it works (Compiles). When I run the “game”, the console continuously writes UnassignedReferenceException:
The variable projectile of FireShell has not been assigned.
You probably need to assign the projectile variable of the FireShell script in the inspector.
I am working off of the tutorial here: gun shooting script c# - Unity Engine - Unity Discussions
I finally kinda got it to fire the right way, but now I’m worrying about that exception. I’m guessing its bad as it might make my program slow or crash?
@DRRosen3 It is rigidbody because the tutorial says to make it rigidbody. Also are you saying to make my tank shell an object? I mean the rigidbody has physics, I have not worked with objects yet so idk.
OH!!! My script has a slot in the inspector for a rigidbody object!! That was what it was asking for, not the rigidbody that you attach to the empty game object to make it shoot. I guess they are both the same. It wont let me attach my rigidbody prefab to the slot!!! But the program runs find without it so what should I do??!!
The one above is an image of my script inspector. The one below is my gameobject that fires the projectile prefab. Both require a rigidbody, yet my script does not allow me to put one in, and complains that I have not set my rigidbody. By the way, my gameobject is inside of a cube.
One reason it might not let you attach it in the inspector, is because you can’t modify a prefab directly. You have to first put the prefab into the scene. Modify it (in the scene) and then “Apply” the changes so they save to the original prefab.
Secondly, what I meant by instantiating an object instead of a rigidbody, is this:
GameObject instantiatedProjectile = Instantiate (projectile, transform.position, transform.rotation) as GameObject;
instantiatedProjectile.rigidbody.velocity = transform.TransformDirection (new Vector3 (0, 0, speed));
This works if the “projectile” is saved as a prefab object, with a rigidbody component attached to it.
Well yes, my “projectile” (The part I am firing out of the square) is just: 1. empty prefab. 2. dragged my 3d tank shell model on top of the prefab icon. 3. applied rigidbody to prefab. 4. dragged the prefab onto my gameobject inspector’s box where it asks for projectile.
This fits in the box, but it doesn’t fit in the other box in the script inspector. Please expand the images posted above, idk why it happens it asks for a rigidbody, clicking the circle won’t let me add it.
Rigidbody is perfectly fine, just like any other type, as long as the actual prefab contains a component of that type. But my impression is that the prefab doesn’t have a rigidbody which leads to the confusion.
I changed it to GameObject, and am now trying to get the shells to destroy when they hit a sphere. Yes, the shells have mesh colliders and rigidbody attached, and my sphere has sphere collider and rigidbody. The shells are going straight through the sphere, coming out the other side, keep on going. IDK what I’m doing wrong. And it still complains that
UnassignedReferenceException:
The variable projectile of FireShell has not been assigned.
You probably need to assign the projectile variable of the FireShell script in the inspector.
Here is my complete code:
using UnityEngine;
using System.Collections;
public class FireShell : MonoBehaviour {
public GameObject projectile;
public float speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1"))
{
Rigidbody instantiatedProjectile = Instantiate (projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection (new Vector3 (0, 0, speed));
}
}
}
and
using UnityEngine;
using System.Collections;
public class DestroyShell : MonoBehaviour {
void OnCollisionEnter(Collision collision)
{
if(collision.transform.tag == "Sphere")
{
Destroy (collision.gameObject);
}
}
}
I attached the 2nd script to my shell, the first one to my gameobject that fires the shells
Well what’s happening is when the shell goes into the sphere it’s destroying the sphere. The line that reads: “void OnCollisionEnter(Collision collision)”… Collision collision stores a reference to what the shell is colliding with, in this case the sphere… So when you say: “Destroy(collision.gameObject);”…what you’re saying is to destroy the sphere. If you want to destroy JUST the shell, then it should say: “Destroy(gameObject);”. If you want to destroy both of them, then just add: “Destroy(gameObject);” after line 9.
this code doesn’t work, and please, I asked about destroying the shell, not firing it.
I appreciate the help, but I have this running well and I’d prefer to not change working things since they don’t work afterwords.
For your UnassignedReferenceException is it possible that you accidentally have multiple instances of the script in your scene?
Add some Debug calls and let us know if the collision is even occurring, and a Debug call in your firing script will let you know if there are multiple instances, as you’ll see multiple debugs for a single shot.
No, my shells don’t move anymore, there is nothing I can do, I am about to give up lol. There are no scripts in my scene other than the one’s attached to my tank gun and my cube ( I mean the empty gameobjects attached to those)
ok. I tried to test if the next line was being executed (the one where it gives the shells velocity) and its not being executed. Actually I get an error at the line before that:
NullReferenceException: Object reference not set to an instance of an object
FireShell.Update () (at Assets/Scripts/FireShell.cs:18)
using UnityEngine;
using System.Collections;
public class FireShell : MonoBehaviour {
public float speed;
public GameObject proj;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1"))
{
Rigidbody instantiatedProjectile = Instantiate (proj, transform.position, transform.rotation) as Rigidbody;
print ("Firing");
instantiatedProjectile.velocity = transform.TransformDirection (new Vector3 (0, 0, speed));
print ("moving");
}
}
}
and then it says:
UnassignedReferenceException: The variable proj of FireShell has not been assigned.
You probably need to assign the proj variable of the FireShell script in the inspector.
And when I click this it points to my empty gameobject attached to my tank barrel. The name is highlighted in yellow, so idk whats wrong, I have set it to be shell prefab