Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Joebear · Dec 08, 2015 at 02:27 AM · javascriptrigidbodyshootingreferenceshoot

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joebear · Dec 08, 2015 at 03:00 AM 0
Share

I forgot to add this, but I'll reward someone who gives me a working answer. Thanks :)

avatar image Joebear · Dec 09, 2015 at 11:01 PM 0
Share

Can someone please answer? This is one of the last steps I need before finishing my game and I can't find the answer.

avatar image jmonasterio · Dec 10, 2015 at 04:25 PM 0
Share

$$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.

avatar image Joebear jmonasterio · Dec 11, 2015 at 12:38 AM 0
Share

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();
     }
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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();
      }
  }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jmonasterio · Dec 11, 2015 at 01:27 AM 0
Share

Yeah, line 4.

avatar image Joebear · Dec 12, 2015 at 01:27 AM 0
Share

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 "?".

avatar image TeohRIK Joebear · Dec 12, 2015 at 02:47 AM 0
Share

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.

avatar image Joebear TeohRIK · Dec 12, 2015 at 02:54 AM 0
Share

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!

avatar image
0

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..

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image odaniel · Sep 14, 2017 at 02:31 PM 0
Share

If there is a Rigidbody component, you can access it in the editor with this code :

GetComponent<Rigidbody>()

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

43 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Car not shooting like bullet properly 2 Answers

Access gameobject from a different scene... 1 Answer

Script removing rigidbody component, NOT game object 1 Answer

Reload only when not firing 1 Answer

Damage drop-off over distance on a physical projectile 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges