Hi guys,
I have a tank, built of a body, a turret parented to it, and a turret launcher object parented to that, which has a script called fireshell.cs and is supposed to shoot when I press space. However, It doesn’t, and gives me the error Object reference not set to an instance of an Object.
I am storing the shell’s speed and damage in its own script, attached to its prefab, and retrieve that data in fireshell. Then I use that information to create a shell prefab and give it a velocity.
However, the shell does not get created, and so here is the code, Error is in line 11/12 of fireshell.
using UnityEngine;
using System.Collections;
public class FireShell : MonoBehaviour {
public Rigidbody proj;
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1"))
{
ShellProperies shellInstance = GetComponent<ShellProperies> (); // not type gameobject
int speed = shellInstance.getSpeed(); // the methods in shellproperties must also be public
Rigidbody instantiatedProjectile = Instantiate (proj, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.rigidbody.AddForce(0,0, 10*speed);
//transform.TransformDirection (new Vector3 (0, 0, speed));
}
}
}
and my other class, the one that stores the shell info.
using UnityEngine;
using System.Collections;
public class ShellProperies : MonoBehaviour {
public int speed;
public int damageItDoes;
public int getSpeed()
{
return speed;
}
public int getDMG()
{
return damageItDoes;
}
}
so ShellProperties script isn’t on the same gameobject as the Fireshell script? thats where your problem is. GetComponent<>() is looking at the current gameobject. You could use GetComponentInChildren<>() instead assuming there is only a single instance of the ShellProperties script in your hierarchy at any one time. If not you need a reference tot he shell gameobject and call
I’m confused about what you mean when you say getcomponentinchildren, idk if the shell is a child of anything, its just a prefab, and idk how I would possible reference my shell gameobject. could you explain?
My shell is literally called shell, and that’s about all there is. I’m not aware of any gameobject classes, and my shell isn’t of any type or anything.
using UnityEngine;
using System.Collections;
public class FireShell : MonoBehaviour {
public Rigidbody proj;
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1"))
{
Rigidbody instantiatedProjectile = Instantiate (proj, transform.position, transform.rotation) as Rigidbody;
ShellProperies shellInstance = instantiatedProjectile.gameObject.GetComponent<ShellProperies> ();
int speed = shellInstance.getSpeed(); // the methods in shellproperties must also be public
instantiatedProjectile.rigidbody.AddForce(0,0, 10*speed);
//transform.TransformDirection (new Vector3 (0, 0, speed));
}
}
}
Actually, it worked! and after changing a bit of code in other scripts, they worked too! So after 2 hits with a shell, a sphere in my game disappears.
Well, thanks for that, now I can pass variables between scripts!
yes I almost do XD, mostly. The only thing now is that I really wanted to make a space game, but will have to start with a game built for tanks… my next steps will be importing new models and then making a “machinegun” so it keeps shooting when u hold down space, instead of having to press it multiple times.