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 /
  • Help Room /
avatar image
0
Question by Negidnyk · Feb 03 at 01:23 PM · ienumerator

How can I pass parameters into IEnumerator method?

I am trying to pass Ray as a parameter of IEnumerator method, but it doesn't change passed object. I would be grateful if someone explain to me why. I thought if variable is reference type it should be changed wherever it is called.


 public class Rays : MonoBehaviour
 {
     private Ray rayR;
     private Ray rayL;
     void Start()
     {
         rayR = new Ray(transform.position + new Vector3(0, -0.8f, 0), transform.up);
         rayL = new Ray(transform.position + new Vector3(0, -0.8f, 0), transform.up);
     }
     void Update()
     {
         Debug.DrawRay(rayR.origin, rayR.direction * 2, Color.blue);
         Debug.DrawRay(rayL.origin, rayL.direction * 2, Color.blue);
         if (Input.GetKeyDown(KeyCode.D))
         {
             rayR.direction = transform.forward;
             StartCoroutine(RayBack(rayR));
         }
         else if (Input.GetKeyDown(KeyCode.A))
         {
             rayL.direction = -transform.forward;
             StartCoroutine(RayBack(rayL));
         }
         if (Physics.Raycast(rayR,2))
         {
             RotateCylinder.rotateRight = false;
         }
         if (Physics.Raycast(rayL, 2))
         {
             RotateCylinder.rotateRight = true;
         }
     }
     IEnumerator RayBack(Ray ray)
     {
         yield return new WaitForSeconds(1);
         Debug.Log("kdv");
         ray.direction = transform.up;
         rayR.direction = transform.up;
     }
 }


Comment
Add comment · Show 4
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 andrew-lukasik · Feb 03 at 01:51 PM 0
Share

Ray is a struct and structs are passed by copy. This means that ray.direction = transform.up; modifies a copy (to no consequence outside of this method scope) meanwhile rayR.direction = transform.up; does something opposite - it modifies a struct that is allocated in memory inside this Rays class.

avatar image Negidnyk · Feb 03 at 02:01 PM 0
Share

@andrew-lukasik , thank you . I just thought that Ray is class therefore reference type . Sorry for disturbing . But how can I pass parameter to IEnumerator be reference , because when I put modifier "ref" it gives an error?

avatar image andrew-lukasik Negidnyk · Feb 03 at 02:28 PM 0
Share

Struct must be contained in some kind of memory allocation object to pass it here. But you can do something different and use System.Action parameter with inlined method (lambda expression)

 StartCoroutine( ExecuteAfterDelay( () => rayR.direction = transform.up , 1f ) );

 IEnumerator ExecuteAfterDelay ( System.Action action , float seconds )
 {
     yield return new WaitForSeconds(seconds);
     action();
 }


avatar image andrew-lukasik Negidnyk · Feb 03 at 02:34 PM 0
Share

Don't overuse it tho. Lambda expressions often allocate garbage memory + has "gotchas" of their own (struct variable capture, boxing, etc.).

0 Replies

· Add your reply
  • Sort: 

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

138 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

Related Questions

Any Way to Improve These Scripts or Increase Game Performance? 0 Answers

Unity Editor can't find IEnumerated 2 Answers

How can i change the value of a variable from an IEnumerator 0 Answers

How can i WaitForSeconds after destroying GameObject to load the next Scene? 0 Answers

IEnumerator, Unexpected symbol? 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