Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Laekh · Jun 27, 2015 at 07:52 PM · 2dstart

Why does my code work in Start but not in another function?

Hello,

For some reason my code works in the Start function but doesn't in another function.

The code I'm executing is as follows:

     var rand = Random.Range(0, 2);
     if(rand <= 0.5) {
         GetComponent.<Rigidbody2D>().AddForce(new Vector2(50, 2));
     } else {
         GetComponent.<Rigidbody2D>().AddForce(new Vector2(-50, -2));
     }

When in function Start(), this works perfectly and the force is added to it nicely. However, I decided I wanted to have a delay on the force, and let my master script handle that instead of the instantiated object where this code is located.

The code in the master script:

 var ball : GameObject;
  
 function Start () {
     Instantiate(ball, 
         mainCam.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, 1f)), 
         Quaternion.identity);
     startGame();
 }
 
 function startGame() {
     yield WaitForSeconds(3);
     ball.GetComponent(BallControl).fireBall();
 }

BallControl is what the script of the first block of code is called.

When the fireBall() function is called, the force doesn't get applied. I know for a fact that it is called, because when I put debug in it, it logs as expected.

Have I done something wrong? How would I fix this issue?

Thank you.

Comment
Add comment
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

2 Replies

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

Answer by Laekh · Jun 27, 2015 at 10:01 PM

I've figured it out.

I think the reason it wouldn't work was that it didn't really know what to apply the force to. Instead of just using GetComponent, I've made it look for the ball.

Working code:

 function FireBall() {
     var rand = Random.Range(0, 2);
     if(rand <= 0.5) {
         GameObject.FindGameObjectWithTag("Ball").GetComponent.<Rigidbody2D>().AddForce(new Vector2(50, 2));
     } else {
         GameObject.FindGameObjectWithTag("Ball").GetComponent.<Rigidbody2D>().AddForce(new Vector2(-50, -2));
     }
 }
Comment
Add comment · Show 2 · 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 Hexer · Jun 27, 2015 at 10:04 PM 1
Share

I'm glad you have figured it out.

avatar image Bunny83 · Jun 27, 2015 at 10:51 PM 0
Share

Of course this will work, but using FindGameObjectWithTag is actually not necessary.

When you call Instantiate you create a clone of the prefab you passed as source. In your original script you then use GetComponent on the prefab and execute your "fireBall" function on the prefab and not on your instantiated object.

Instantiate returns a reference to the newly created object. That's the one you want to use ins$$anonymous$$d.

 var ball : GameObject;
 private var instance : GameObject;
   
 function Start ()
 {
     instance = Instantiate(ball, 
         mainCam.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, 1f)), 
         Quaternion.identity);
     startGame();
 }
 
 function startGame() {
     yield WaitForSeconds(3);
     instance.GetComponent(BallControl).fireBall();
 }

avatar image
0

Answer by Hexer · Jun 27, 2015 at 08:46 PM

You haven't invoke the function that applies the force, in your second script. You have only told the script to get the script object and nothing else.

Add something like this, in the "startGame" function.

 BallControl.Invoke("NameOfFunction",0.1f);

I mainly use C# but this should work, I have done JS before. Don't forget to change NameOfFunction to the desired function name. In this case it would be the function where your force script logic is put.

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 Laekh · Jun 27, 2015 at 08:55 PM 0
Share

Thank you, but this doesn't seem to do the trick for me. The weird thing is, it's all called, but the force just doesn't get applied for some reason.

Code:

 ball.GetComponent(BallControl).Invoke("FireBall", 3);

Code it calls after the 3 second delay:

 function FireBall() {
     var rand = Random.Range(0, 2);
     if(rand <= 0.5) {
         Debug.Log("Fired to the left");
         GetComponent.<Rigidbody2D>().AddForce(new Vector2(50, 2));
     } else {
         Debug.Log("Fired to the right");
         GetComponent.<Rigidbody2D>().AddForce(new Vector2(-50, -2));
     }
 }

It prints the debug but doesn't apply the force, it however does apply the force whenever the same exact code is called from the Start function.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Start() is called 2 times when Instantiating prefab 2 Answers

2D Animation does not start 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Where to start? 3 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