- Home /
Found the solution
Can someone help me with calling a method from another Game Object?
I am trying to build an Asteroid game and been following a tutorial. And since I am a complete beginner the following piece of code may have some redundancy however that is there to keep things very simple.
I have an AsteroidSpawner script which is attached to the main Camera. It instantiates an Asteroid and calls the Initialize method of Asteroid script attached to the prefab Asteroid. Following is the code:
AsteroidSpawner:
void Start ()
{
Vector3 location = new Vector3(0, 0, -Camera.main.transform.position.z);
Instantiate<GameObject>(prefabAsteroid, location, Quaternion.identity);
prefabAsteroid.GetComponent<asteroid>().Initialize(parameter);
}
Asteroid:
public void Initialize(int parameter)
{
const float MinImpulseForce = 3f;
const float MaxImpulseForce = 5f;
float angle = Random.Range(0, 30 * Mathf.Deg2Rad);
Vector2 direction = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
float magnitude = Random.Range(MinImpulseForce, MaxImpulseForce);
gameObject.GetComponent<Rigidbody2D>().AddForce(direction * magnitude, ForceMode2D.Impulse);
print(parameter);
}
The above game when played does instantiate an Asteroid game object as per the given location but doesn't move. However, when the same Initialize code is pasted to the start() method of Asteroid it works just as expected.
I know a possible fix is to copy the code to start() but I am passing a parameter the functionality of which has been avoided here to reduce complexity. Explanation of the above-unexpected output would be highly appreciated instead of alternatives. Thanks!
UPDATE: After messing around a bit I found out that the initialize method is getting called before the start method and maybe that is a possible reason for no forced being added. Can someone suggest a fix to that if this is indeed the case?
I have verified that Initialize() method does get called and both direction and magnitude have an acceptable value. Asteroid prefab does have a sprite, rigidbody2D and a collider2D attached to it. And since Asteroid does move with the expected value of force when Initialize() code is written to the start() method I have no idea where am I going wrong.
Answer by SamuelGoldenbaum · Nov 17, 2018 at 07:14 PM
Not clear to advise, but would suggest you study: https://docs.unity3d.com/Manual/ExecutionOrder.html to help understand when events are being called
I went through that a while back however it talks about the execution order of pre-existing events. Over here from the few testing I did, I concluded that when the object is Instantiated start() method isn't called instantly and ins$$anonymous$$d Initialize() [which is a user defined method] gets called before start() does.
Still thanks for the help! :)
Follow this Question
Related Questions
Missing (Game Object) 3 Answers
GUIText transition trouble 1 Answer
How can I add the OnTriggerEnter function to all game objects that I instantiate? 1 Answer
How can I disable a Method. 1 Answer
There is a generation lag 1 Answer