I realize this has been brought up in other topics, but I am having issues with the coconutthrow script from Unity Game Development Fundamentals. Here’s my script:
var throwSound : AudioClip;
var coconutObject : Rigidbody;
var throwForce : float;
function Update () {
if(Input.GetButtonUp("Fire1")){
audio.PlayOneShot(throwSound);
var newCoconut : Rigidbody = Instantiate(coconutObject, transform.position, transform.rotation);
newCoconut.rigidbody.velocity = transform.TransformDirection(Vector3(0,0, throwForce));
newCoconut.name = "coconut";
}
if(!newCoconut.rigidbody) {
newCoconut.AddComponent(Rigidbody);
Physics.IgnoreCollision(transform.root.collider, newCoconut.collider, true);
}
}
@script RequireComponent (AudioSource)
I get the error message:
Assets/Scripts/CoconutThrow.js(15,28 ): BCE0019: ‘AddComponent’ is not a member of ‘UnityEngine.Rigidbody’.
Can anyone tell me why this is? It seems to be an ongoing problem I’ve noticed a few people having this issue but not saying how they fixed it…
Haven’t gotten to scripting, and to tell the truth I am very limited in my knowledge of programming altogether, but according to this page Unity - Scripting API: GameObject.AddComponent
the syntax is as follows.
newCoconut.AddComponent(“Rigidbody”);
So maybe if you change it to look like this it might fix the problem.?
The newCoconut variable is of type Rigidbody, but the AddComponent function is actually a member of the GameObject class. If you declare newCoconut as a GameObject instead of a rigidbody, the script should be OK:-
Is not needed. It says in the book that since you have applied a rigidbody to your coconut prefab that you don’t need this code since it basically just adds a rigidbody to new instances of coconuts that are made during gameplay by the script.
The second problem is that since you thought that the code was supposed to be added, and that the next part of the book says to add the physics.ignorecollisions bit of code after the last bit of code you typed, you put the physics.ignorecollision bit of code after the code you didn’t need to put in. So you have to move that part of the code back up into the main IF area.
So this is what your code should look like:
var throwSound : AudioClip;
var coconutObject : Rigidbody;
var throwForce : float;
function Update () {
if (Input.GetButtonUp("Fire1")){
audio.PlayOneShot (throwSound);
var newCoconut : Rigidbody = Instantiate(coconutObject, transform.position, transform.rotation);
newCoconut.name = "coconut";
newCoconut.rigidbody.velocity = transform.TransformDirection(Vector3(0, 0, throwForce));
Physics.IgnoreCollision(transform.root.collider, newCoconut.collider, true);
}
}
@script RequireComponent(AudioSource)
You’ll notice I took out the part that was not needed and moved the Physics.Ignorecollision section up into the main IF statement.
Hi guys, i’m still learning Unity3D so i’m not sure about this, but from what i’ve read until now shouldn’t this script be placed inside FixedUpdate() if we are working with a rigidbody?
Thank you for that script, it’s cleared up all my console errors. The only thing is my coconuts only throw in one direction and at one angle no matter which way the FPC faces. Previously (before the CoconutCollision confusion) my coconuts fired all over the place very happily.
Any idea what’s going on?
Thanks a million!