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 /
avatar image
0
Question by jast26 · Sep 03, 2018 at 09:58 AM · inputcoroutinevariables

fixing a variable value during coroutine

hello, i will briefly explain my situation: i have a script that:

  • gets the mouse position twice;

  • calculates the magnitude and the angle of the line between the two points;

  • istantiates a prefab at the first position

  • a coroutine that scales the prefab along the line (var lengthC)

the problem is that when i istantiate a new object before the scaling of the other object is complete, i suppose that the variable lengthC gets updated and the previous coroutine stops. is there a way to get around it? can i like call the variable inside the coroutine and while it is executing the variable will have a fixed value?

here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpawnaCresci : MonoBehaviour
 {
 
     [SerializeField] GameObject myCube;
     [SerializeField] float width = 1f;
     [SerializeField] float height = 1f;
     [SerializeField] Vector3 inputPosA = Vector3.zero;
     [SerializeField] Vector3 inputPosB = Vector3.zero;
     [SerializeField] Vector3 result;
     [SerializeField] Vector3 avoidBehindCamera;
     private GameObject thisCube;
     private float lengthC;
     [SerializeField] float growFactor = 0.1f;
 
     // Use this for initialization
     void Start()
     {
         avoidBehindCamera.z = 10f;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
 
             inputPosA = Camera.main.ScreenToWorldPoint(Input.mousePosition + avoidBehindCamera);
         }
 
         if (Input.GetMouseButtonUp(0))
         {
             inputPosB = Camera.main.ScreenToWorldPoint(Input.mousePosition + avoidBehindCamera);
 
             SpawnAlongLine();
             
         }
 
 
     }
 
     void SpawnAlongLine()
     {
        // Vector3 posC = ((inputPosB - inputPosA) * 0.5F) + inputPosA;
         lengthC = (inputPosB - inputPosA).magnitude;
         float sineC = (inputPosB.y - inputPosA.y) / lengthC;
         float angleC = Mathf.Asin(sineC) * Mathf.Rad2Deg;
         if (inputPosB.x < inputPosA.x) { angleC = 0 - angleC; }
 
         Debug.Log("inputPosA" + inputPosA + " : inputPosB" + inputPosB + /*" : posC" + posC + */" : lengthC " + lengthC + " : sineC " + sineC + " : angleC " + angleC);
 
         thisCube = Object.Instantiate(myCube, inputPosA, Quaternion.identity);
 
         thisCube.transform.rotation = Quaternion.Euler(0, 0, angleC);
         thisCube.transform.parent = null;
 
         StartCoroutine(Scale());
 
     }
 
     IEnumerator Scale()
     {
         float timer = 0;
 
         while (lengthC > thisCube.transform.localScale.x)
               {
                 timer = Time.deltaTime;
                 thisCube.transform.localScale += new Vector3(1, 0, 0) * timer * growFactor ;
             thisCube.transform.localPosition += thisCube.transform.right * timer * growFactor * 0.5f;
             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
0

Answer by Casiell · Sep 03, 2018 at 10:30 AM

You can pass the value as a paramether to your coroutine. Or create local variable inside the coroutine and assign the value from lengthC at the beginning.

First solution is better in this case

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 jast26 · Sep 03, 2018 at 10:48 AM 0
Share

sorry, i'm a total noob and i have never used coroutines on my own before; isn't this the way to pass a parameter?

  StartCoroutine(Scale(lengthC));
    
 }
 
 IEnumerator Scale(float lengthC)
 {
     float timer = 0;
 
     while (lengthC > thisCube.transform.localScale.x)
           {
             timer = Time.deltaTime;
             thisCube.transform.localScale += new Vector3(1, 0, 0) * timer * growFactor ;
         thisCube.transform.localPosition += thisCube.transform.right * timer * growFactor * 0.5f;
         yield return null;
           }
    
 }


because it is not working :(

avatar image jast26 · Sep 03, 2018 at 11:00 AM 0
Share

i have also noticed that the velocity of the transform builds up if the previous coroutine has not yet finished! :(

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

112 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

Related Questions

Functions with variables 1 Answer

How to prevent holding down a key to infinitely loop an action? 1 Answer

Scripting problem, with values and input.getbutton 1 Answer

How does WaitForSeconds pass values? 0 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 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