Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 braisque · Apr 20, 2019 at 06:58 PM · raycastscript.coroutinenon-static

How to get non static values from object A and inject them in a coroutine in object B ?

I made a raycast that check if a door should load a new scene. When the player click while the ray hit the door, it starts a coroutine that initiate various action to load the new scene. It works, but...

I wish to tie some non static datas to these doors so it send the player to the appropriate scene and place just by changing values in the inspector of my door object. Those datas would be a string (the name of the room), 3 coordinates (x, y, z), and a sound effect (an audiosource, then).

So, I have this very basic script that looks like that, and which I put in every door object:

 using UnityEngine;
 
 public class SceneChanger : MonoBehaviour
 {
     public string Location;
     public int x_coordinate;
     public int y_coordinate;
     public int z_coordinate;
     public AudioSource RoomChangeSound;
 }

I can access these value from an other object, the interact script of my player, by typing something like that, below. The problem is that I can't take advantage of my coroutines this way.

    void Update()
     {
             if (hit.collider.CompareTag("change_scene"))
             {
                 if (Input.GetKeyDown(KeyCode.Mouse0))
                 {
                     Initiate.Fade(hit.collider.GetComponent<SceneChanger>().Location, Color.black, 1.5f);
                     StartCoroutine(change_scene());
             }
 }

And my corroutine looks like that without all the useless stuff:

     IEnumerator change_scene()
     {
         Initiate.Fade("room_string", Color.black, 1.5f); /// This line is important
         while (!Fader.startedLoading)
         {
             yield return null;
         }
         transform.parent.position = new Vector3(1f, 1.06f, 4f); ///And this one too
         while (Initiate.areWeFading)
         {
             yield return null;
         }
     }

So now, what I'd like to do is get the values from the first script (the coordinates and the string) when I click on a door (with my raycast), and then add these value in the two important line marked in the last script. How can I do that? Because I just can't seem to use the "hit" of my raycast in the coroutine...

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 braisque · Apr 20, 2019 at 09:02 PM 0
Share

Solved... Actually I just had to write it down to get the answer by myself. I was just... tired, I guess. Just put that in the interact script of the player, so it'll access the variables from another script, store them, and then you can use these new variables (NewLockX, NewRoom, etc.) in your coroutine.

             if (hit.collider.CompareTag("change_scene"))
             {
                 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0))
                 {
                     NewRoom = hit.collider.GetComponent<SceneChanger>().Location;
                     NewLocX = hit.collider.GetComponent<SceneChanger>().x_coordinate;
                     StartCoroutine(change_scene());
                 }
             }

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

161 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

Related Questions

Why does ScreenPointToRay bug when called from IEnumerators? 1 Answer

colliders vs raycast vs physics.overlap? 0 Answers

Cancel Input if the direction has a wall in the way 1 Answer

Animation sometimes doesn't play until the end, sometimes it doesn't play at all and sometimes it plays more than once. 1 Answer

How to differentiate objects with the same script? 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