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
0
Question by henbarlevi · Jan 27, 2016 at 06:03 PM · c#rigidbody2dprefabsclone

How do i take a grab of clone from prefab and change his velocity

im creating2d ufo game and im trying to make the spaceship fire i created a prefab of bullet and add a rigidbody component. created an empty object in the hierarchy and attached the script to it. the creation of bullets from the prefab succeded but i cant write the right code in order to manipulet each cloneBullet velocity. they stay static on the screen i tried in my code to make each bullet move the same direction just for testing it. it doesnt work. and can u give some direction how to manipulet all bullets in each frame alt text

this is my code: using UnityEngine; using System.Collections;

public class FireController : MonoBehaviour { public Transform Bullet; public GameObject player; private GameObject Bulletclone; string direction; // Use this for initialization void Start () {

 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown(KeyCode.F))
     {
         Bulletclone = Instantiate(Bullet, player.transform.position, Bullet.rotation) as GameObject; 
         Bulletclone.GetComponent<Rigidbody2D>().velocity = new Vector2(0, 20);
         
     }


new-bitmap-image.jpg (320.2 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by FortisVenaliter · Jan 27, 2016 at 06:16 PM

You should use RigidBody's AddForce() function rather than setting the velocity directly. Also, make sure there aren't other colliders where it spawns that will block it from moving.

Comment
Add comment · Show 5 · 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 henbarlevi · Jan 27, 2016 at 07:01 PM 0
Share

tried it. and he still saynig the error: "object reference not set to an instance of an object" on the line im getting the rigidbody2d component of the clone

avatar image FortisVenaliter · Jan 27, 2016 at 07:04 PM 0
Share

Oh, well a Null Reference Exception is a whole different thing. That means that you don't have a Rigidbody2D attached to the bullet. Do you have one on the prefab?

avatar image henbarlevi · Jan 27, 2016 at 07:35 PM 0
Share

yea i did. you can see it in the image. i dont have a clue why it saying that

avatar image henbarlevi · Jan 28, 2016 at 10:31 AM 0
Share

tried it. and he still saynig the error: "object reference not set to an instance of an object" on the line im getting the rigidbody2d component of the clone

avatar image FortisVenaliter henbarlevi · Jan 28, 2016 at 03:56 PM 0
Share

Just out of curiosity... Could you try instantiating Bullet.gameObject ins$$anonymous$$d of Bullet and see if that makes a difference?

avatar image
0

Answer by henbarlevi · Jan 29, 2016 at 03:10 PM

the problem was that i decleared Bullet as Transform instead of a gameobject. Fixed it!. thx for the help!

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rigidbody is not Jumping while Moving 0 Answers

how would I change player.transform.RotateAround to MoveRotation 0 Answers

Make Prefab Rotate Depending on a different Game Object's Rotation. 0 Answers

How can you load next the level after you destroy the last clone c# 1 Answer

Passing variables from one script to all clones 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