I’m a new user of the Unity engine and I’m having trouble with a grenade lobbing Javascript. I’m attempting to adapt the code I found in Will Goldstone’s book “Unity Game Development Essentials” for use by both players and AI controlled enemies. The trouble is that I cannot get the script to compile because there numerous errors. Here is what I have:
GrenadeToss.js
var throwSound : AudioClip;
var throwForce : float;
private var grenadePrefab : GameObject;
private var grenadier : GameObject;
static function LobGrenade(){
audio.PlayOneShot (throwSound);
// instantiate and configure an object to throw
var newGrenade : GameObject = Instantiate(grenadePrefab, grenadier.transform.position, grenadier.transform.rotation);
newGrenade.name = "grenade";
newGrenade.AddComponent(Rigidbody);
newGrenade.rigidbody.velocity = grenadier.transform.TransformDirection(Vector3(0,0, throwForce));
//prevent grenade collisions with the attacker's collider
Physics.IgnoreCollision(grenadier.transform.collider, newGrenade.collider, true);
}
@script RequireComponent(AudioSource)
The errors I get are:
I’ve search these forums for a solution, but did not see any that pertained to the way the aforementioned book suggests to code this.
After posting the request above, I found and corrected part of the problem. Here is what I changed:
Original:
var throwSound : AudioClip;
var throwForce : float;
private var grenadePrefab : GameObject;
private var grenadier : GameObject;
static function LobGrenade(){
New:
var throwSound : AudioClip;
var grenadePrefab : GameObject;
var throwForce : float;
static function LobGrenade(grenadier : GameObject){
If it’s a static function, it can’t access variables like audio or transform. The static function can be called from anywhere, so it can’t assume you have them.
If you want to use non-static variables in your static function, you’ll need to use a reference to a non-static version of the script. Basically, you’d create a static var myself : GrenadeToss; and set that static variable to whoever is the grenadier. You’d need an instance of the script, of course.
Why is the function Static, though? Your variable for who throws the grenade is private, so only the script itself can access or change it. Despite that, you’re trying to use it statically. You’d be better off with a non-static function and each user of the script having its own instance.
Edit: Or you could do what you did above and take it further, by sending each of the global variables as variables of the function.
I moved grenadePrefab and throwForce into the function prototype in case to accommodate different grenade models (bottles, sticks, chairs, etc) and a little character-side variance. I also removed the audio component since I will handle that part elsewhere. The function now looks like this:
static function LobGrenade(grenadier : GameObject, grenadePrefab : GameObject, throwForce : float){
// instantiate and configure an object to throw
var newGrenade : GameObject = Instantiate(grenadePrefab, grenadier.transform.position, grenadier.transform.rotation);
newGrenade.name = "grenade";
// in case we get a grenade prefab that does not have a rigidbody component
if(!newGrenade.rigidbody){
newGrenade.AddComponent(Rigidbody);
}
newGrenade.rigidbody.velocity = grenadier.transform.TransformDirection(Vector3(0,0, throwForce));
//prevent grenade collisions with the attacker's collider
Physics.IgnoreCollision(grenadier.transform.collider, newGrenade.collider, true);
}
Now all the compile errors are gone, but I am getting a “NullRefereceException” during runtime. I’ve investigated this and found that my method of obtaining the player’s GameObject is not working.
Sounds like GameObject.Find(“Player”) is not functioning correctly. Do you have an object in your scene with the name “Player” or do you have an object tagged with “Player”?
EDIT:
An easy way out (and faster performance wise) would be to put var player : GameObject; at the top of your script and assign it from the inspector. Then you would replace GameObject.Find(“Player”) with player.
An easy way would be to make a variable in the script you want to call that function from that you set equal to the script with the function in it, and call the function something like below:
var myScript; //assign this to the script that has the function you want to call in it