- Home /
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?
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.
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.
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
Follow this Question
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