Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Jmuoi · Feb 20, 2021 at 01:07 AM · enable and disable script

After disabling then enabling this script it stops working anyone know why?

i used unity playmaker to disable and enable the script

using UnityEngine; using System.Collections;

public class Green : MonoBehaviour { public Transform Target; public float firingAngle = 45.0f; public float gravity = 9.8f;

 public Transform Projectile;
 private Transform myTransform;

 void Awake()
 {
     myTransform = transform;
 }

 void Start()
 {
     StartCoroutine(SimulateProjectile());
 }


 IEnumerator SimulateProjectile()
 {
     // Short delay added before Projectile is thrown
     yield return new WaitForSeconds(0.1f);

     // Move projectile to the position of throwing object + add some offset if needed.
     Projectile.position = myTransform.position + new Vector3(0, 0.0f, 0);

     // Calculate distance to target
     float target_Distance = Vector3.Distance(Projectile.position, Target.position);

     // Calculate the velocity needed to throw the object to the target at specified angle.
     float projectile_Velocity = target_Distance / (Mathf.Sin(2 * firingAngle * Mathf.Deg2Rad) / gravity);

     // Extract the X  Y componenent of the velocity
     float Vx = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(firingAngle * Mathf.Deg2Rad);
     float Vy = Mathf.Sqrt(projectile_Velocity) * Mathf.Sin(firingAngle * Mathf.Deg2Rad);

     // Calculate flight time.
     float flightDuration = target_Distance / Vx;

     // Rotate projectile to face the target.
     Projectile.rotation = Quaternion.LookRotation(Target.position - Projectile.position);

     float elapse_time = 0;

     while (elapse_time < flightDuration)
     {
         Projectile.Translate(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime);

         elapse_time += Time.deltaTime;

         yield return null;
     }
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by MUG806 · Feb 20, 2021 at 03:42 PM

Not 100% sure but I'd try moving "StartCoroutine(SimulateProjectile());" from Start() to OnEnable(). That way your coroutine should start when you enable the script, not just when it is first created. You may also need to add OnDisable() with "StopCoroutine(SimulateProjectile());", if you want the coroutine to cancel when the object is disabled.

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 Llama_w_2Ls · Feb 20, 2021 at 04:24 PM 0
Share

The start method on all monobehaviour scripts run when the scene is first loaded. It isn't ever called again. Even if you disable then enable the script. OnEnable is specific to the script, so it should work as intended.

avatar image MUG806 Llama_w_2Ls · Feb 20, 2021 at 04:31 PM 0
Share

Which is exactly why I suggested not using start...

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

113 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

Related Questions

Script has been enabled during the gameplay, but never called. 0 Answers

BCE0019 "enabled" is not a member of "Object"(On JavaScript Script) 2 Answers

How do I enable my scripts on default?? 3 Answers

Script isn't disabling? 1 Answer

About takeover control 0 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