AddRigidBody Script Error
I have an AddRigidBody script for an FPS zombie game, and it has this error. I'm not even really sure what this means, nor can I find it anywhere on the internet. The error: " Assets/Scripts/AddRigidBody.cs(8,17): error CS0029: Cannot implicitly convert type UnityEngine.Rigidbody' to
AddRigidbody' " And my script:
using UnityEngine;
using System.Collections;
public class AddRigidbody: MonoBehaviour {
public float mass;
public AddRigidbody rb;
void Start() {
rb = GetComponent<Rigidbody>();
rb.mass = mass;
}
}
Just to clear it up, both of the objects have non-trigger collisions. The object is a zombie and the second object is a wall. They just kind of go right through the walls, instead of stopping like the Player. Thanks
Answer by Firedan1176 · Dec 16, 2015 at 05:01 AM
On line 6, you are trying to set your gameObject's Rigid body as an AddRigidBody, so you shold change that line to public Rigidbody rb;
. I'm not sure if you're familiar with objects, but when you create a class, you can create a new object (which isn't Unity specific, like GameObject), which is why you would use new GameObject ()
or in your case, new AddRigidBody ()
, so I hope this clears some things up for you.
The error essentially means that the data types aren't the same, and Unity is not able to convert them (for example, float to a double or even a string).
I fixed the error, but I can't add the component. It says I need a valid script. Your comment did help me a lot, thank you! But I still can't assess the script onto the enemy. There are no errors in the log, and the file name and script class are the same. I'm not sure what's causing this. Or is this a different question to be posted? Thank you for your input, though!
you should update your script in the post (or add how it looks now) because if your script is invalid we cant help you without seeing the script.
from what Firedan1176 said it should look like:
using UnityEngine;
using System.Collections;
public class AddRigidbody: $$anonymous$$onoBehaviour {
public float mass;
public Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody>();
rb.mass = mass;
}
}
Your answer
Follow this Question
Related Questions
Why Does A Rigidbody Affect Enemy Health? 1 Answer
error CS0103 rb doesn't exist in the current context, ligne 8 and 15 , can't fix it 1 Answer
How to move an object able to collide through something outside of physics? 0 Answers
Freeze rotation of just the box collider component 0 Answers
How to fix this problem? 1 Answer