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 RandomCitizens · Sep 26, 2017 at 02:57 PM · rigidbody2dprefabsinstancerigidbody.addforce

Rigidbody.addforce not working with prefab

So I have a prefab called asteroid, which has a script attached named AsteroidInit.cs that initializes various aspects of the asteroid. I also have another script called GameInfo, which I am using to store various important game variables. My script structure is as follows:

 [RequireComponent(typeof(Rigidbody2D))]
 
 public class AsteroidInit : MonoBehaviour {    
 
 //objects and components to reference
     private GameObject go;
     private Transform trans;
     private GameInfo GI;
     private Rigidbody2D rb;
  //The indicidual asteroid variables
     private int rotoSpeed;
     private int moveSpeed;
     private int astroHealth;
     private int value;
     private int children;
     private int typeNum;
 
 //Function to set the various values for the astroid references GameInfo script.
 
 private void SetAsteroidValues( GameObject asteroidObj, GameInfo GI)
 {
    //This section would be too long to be worth posting here.
    //The values are being set properly anyway, I have checked.
 }
 
     void Awake()
 {
     //Get references
     go = this.gameObject;
     trans = go.transform;
     GI = FindObjectOfType<GameInfo>();
     rb = go.GetComponent<Rigidbody2D>();

     //set values
     SetAstroidValues(go, GI);
 }

 void Start()
 {
     //set initial velocity
     rb.AddRelativeForce(new Vector3(0, moveSpeed, 0));//This doesn't work
     //set rotation
     trans.rotation = Random.rotation;//This doesn't work
 }
    void Update()
     {
         //rotate each frame
         trans.eulerAngles = new Vector2( 0, (trans.eulerAngles.z + rotoSpeed) * Time.deltaTime);//This also doesn't work
     }

This script is attached to an Astroid prefab with an attached Rigidbody. The Astroid prefab is Intantiated by another script called GameManager at origin.

Why are only certain portions of the script executing on the prefabs? I have used Debug.Log to check and see if the values stored in the GameInfo script were the problem, but they seem to be being set correctly. Additionally, after placing some Debug.Log calls in Start() and Update(), both seem to be executing. So whats up?

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 RandomCitizens · Sep 26, 2017 at 03:34 PM 0
Share

Interesting, I manually created a copy of the prefab object (as in constructed it from an empty game object) and it still didn't work. The object was rendered, however, the rotation and addforce seem to no have executed as the object didn't move.

The original copy made from the prefab did work though, which is strange.

1 Reply

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

Answer by RandomCitizens · Sep 26, 2017 at 03:40 PM

I am an idiot. In the SetAsteroidValues() function I was setting the values based on the name of the asteroid. Which was fine for the first asteroid, however, each clone had the "(Clone)" added to their name. Ah well, live and learn I guess.

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 JonPQ · Sep 26, 2017 at 04:05 PM 0
Share

tip: avoid GameObject.Find("name"); its super slow ! ins$$anonymous$$d when you instantiate it.. just keep a reference to each asteroid... maybe in a list or array.
gameObject obj = gameObject .instantiate(asteroidPrefab); asteroidList.add(obj);

something like that

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

71 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

Related Questions

Prefab is lost in between 1 Answer

Rigidbody2D.AddForce can't change X velocity 1 Answer

How to get acceleration in a top-down angle 0 Answers

AddForce gradually decreasing 0 Answers

AddRelativeForce with a rigidbody2D ? 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