I can't get my rigidbody reference set on my script in the editor?
This is a really stupid and noobish question, I'm sure, but I can't seem to find an answer. Under my public script display, where I would add an object to be a bullet (bullet = None (Rigidbody) I see nothing, while on my other displays I usually see a list of all of my objects. This says only "none." I also used the drag-and-drop method to apply the object, but it didn't work. I'm sure I'm missing something really obvious with this, but I figured there was no harm in asking while I look.
I forgot to add this, but I'll reward someone who gives me a working answer. Thanks :)
Can someone please answer? This is one of the last steps I need before finishing my game and I can't find the answer.
$$anonymous$$aybe post the script? (At least the part where you declare the "bullet" property.).
You may have put a "private" access modifier on the bullet, or something that make it not display in the Inspector.
This is in C#, which I don't know much about, but I was told to use it as a starting script. I converted to it from Javascript, (which I also knew close to nothing about), so I'm sure I have something in here that's JavaScript and not C#. using UnityEngine; using System.Collections;
public class Shooting : $$anonymous$$onoBehaviour
{
public float bulletSpeed = 10;
public Rigidbody bullet;
void Fire()
{
Rigidbody bulletClone = (Rigidbody) Instantiate(bullet, transform.position, transform.rotation);
bulletClone.velocity = transform.forward * bulletSpeed;
}
void Update ()
{
if (Input.GetButtonDown("Fire1"))
Fire();
}
}
Answer by TeohRIK · Dec 11, 2015 at 12:57 AM
Ok, after looking on your script, I think it should be like this
public class Shooting : MonoBehaviour
{
public float bulletSpeed = 10;
public GameObject bullet;
void Fire()
{
GameObject bulletClone = Instantiate(bullet, transform.position, transform.rotation) as GameObject;
bulletClone.GetComponent<Rigidbody>().velocity = transform.forward * bulletSpeed;
}
void Update ()
{
if (Input.GetButtonDown("Fire1"))
Fire();
}
}
I assume line 10 (13 for me, because of the "using Unity Engine" lines) would have RigidBody replaced with GameObject, since it was like that on line 4? Would that act the same as a component as it does an object? Either way, it doesn't recognize Velocity as any kind of construct whatsoever. With your script, I would scroll over "Rigidbody" and it would say it doesn't exist in the current context. This is fixed with GameObject, but when Velocity is scrolled over, it just says "?".
opps, sorry, the B for the RigidBody should be in small capital letter. So it should be something like this
bulletClone.GetComponent<Rigidbody>().velocity = transform.forward * bulletSpeed;
Btw, maybe you should understand what is and the function of GetComponent, it something like a pointer that use to get the component you want from the game object. Because now you to add velocity, but velocity is under the function of rigidbody, so I pass in Rigidbody, if you pass in GameObject, you get back the samething again.
Wow, thanks. That helped a lot! I will definitely look at the tutorials. That's what I was doing with this before, but I couldn't seem to find one that focused on this topic. Happy holidays!
Answer by Arslan2171 · Aug 21, 2017 at 08:57 AM
I cant acces my rigidbody and renderd object and included some more things in my script editor will you please told me whts the problm with this ... i m stuck off in this point since 7days and i cant get the solution of this problem is their any file missing or some other problem occured help me guys..
If there is a Rigidbody component, you can access it in the editor with this code :
GetComponent<Rigidbody>()