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 /
avatar image
0
Question by mauriciomb · Jan 03, 2018 at 03:33 AM · rigidbody2dobjects-movement

trying to move an object with no script by accessing its rigidbody2d component.

I have a slime sprite that will instantiate and fire a slime ball. As stated above, I am trying to move the slime ball by using velocity on its rigidbody component. The problem is that it will only instantiate without moving at all. I am getting zero errors and am very puzzled as to why my code is not working.

     private bool SpawnBall;
         public GameObject SlimeBallAttack;
         public float slimeballX;
         public float slimeballY;
         public float slimeballSpeed;
         public float ballVerticality;
         public SlimeBallMove ballVelocity;
 
 void Start()
     {
         SlimeE = GetComponent<Rigidbody2D>();
         SlimeAnim = GetComponent<Animator>();
         currentEnemyState = EnemyState.Idle;
         mySpriteRenderer = GetComponent<SpriteRenderer>();
         risk = 0;
         random = true;
         
         
     }
 
         void Attack()
             {
                 InvokeRepeating("RandomValue", 1f, 1f);
             }
             private void RandomValue ()
             {
                 
                 risk = Random.Range(0, 11);
                 attackMeter += risk;
         
                 if (attackMeter >= 20 && SlimeAnim.GetInteger("SlimeAttack") == 1)
                 {
                     attackMeter = 0;
                     SpawnBall = true;
                 }
                 if (SpawnBall == true)
                 {
                     if(mySpriteRenderer.flipX == false)
                     {
                        Instantiate(SlimeBallAttack, new Vector2(gameObject.transform.position.x + slimeballX, gameObject.transform.position.y + slimeballY), gameObject.transform.rotation);
                        SlimeBallAttack.GetComponent<Rigidbody2D>().velocity = new Vector2(slimeballSpeed * Time.deltaTime, ballVerticality);
                     }
         
                     if (mySpriteRenderer.flipX == true)
                     {
                         Instantiate(SlimeBallAttack, new Vector2(gameObject.transform.position.x - slimeballX, gameObject.transform.position.y + slimeballY), gameObject.transform.rotation);
                        SlimeBallAttack.GetComponent<Rigidbody2D>().velocity = new Vector2(-slimeballSpeed * Time.deltaTime, ballVerticality);
                     }
                     SpawnBall = false;
                 }


NOTE: the Attack() function is in update and is only called if a certain condition passes. Also, I have tried putting the code to instantiate and move the object in update but it is only instantiating with no movement.

Comment
Add comment · Show 1
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 dev-waqas · Jan 03, 2018 at 05:09 AM 0
Share

Check your time scale of game.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Lysander · Jan 03, 2018 at 05:56 AM

You're trying to use functions on the components attached to the Prefab, not the object you just instantiated. These are two different objects. Instantiate returns a GameObject, so just assign that to a temporary variable, then use your GetComponent and such on that.

 GameObject newSlime = Instantiate<GameObject>(SlimeBallAttack, new Vector2(gameObject.transform.position.x + slimeballX, gameObject.transform.position.y + slimeballY), gameObject.transform.rotation);
 
 newSlime.GetComponent<Rigidbody2D>().velocity = new Vector2(slimeballSpeed * Time.deltaTime, ballVerticality);
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 mauriciomb · Jan 03, 2018 at 03:35 PM 0
Share

Thanks so much, that was exactly what was wrong. Now I understand prefabs better :D

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

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

Object not moving according to the meshes..? 0 Answers

How to make a gameobject with rigidbody2D fly through the air? 2 Answers

Adding force to object hit by sword 0 Answers

How do add force to the left (x-axis) 2 Answers

Adding a child problem 0 Answers


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