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 /
This question was closed Nov 17, 2018 at 08:27 PM by mbaheti for the following reason:

Found the solution

avatar image
0
Question by mbaheti · Nov 17, 2018 at 06:50 PM · scripting probleminstantiatemethodgame object

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?

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 mbaheti · Nov 17, 2018 at 06:56 PM 0
Share

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.

1 Reply

  • Sort: 
avatar image
0

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

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 mbaheti · Nov 17, 2018 at 07:21 PM 0
Share

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

Answers Answers and Comments

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

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


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