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 /
  • Help Room /
avatar image
0
Question by jonorgan26 · Jan 30, 2020 at 02:21 PM · scripting problemrigidbodytransform

Using components through a coroutine not working

I have this code:

 void getOnShip()
 {
   boatMove scr = boatAtBase.GetComponent<boatMove>();
   Debug.Log("Get on ship button clicked.");
   GameObject[] navies = GameObject.FindGameObjectsWithTag("Navy");
   boatAtBase.transform.parent = null;
   for (int i = 0; scr.capacity < scr.capacityLimit; i++ )
   {
     sendNavaltoBoat(navies[i]);
     scr.upCap();
   }
 }

 void sendNavaltoBoat(GameObject nav)
 {
   Debug.Log("send to naval called");
   Transform ntf = nav.GetComponent<Transform>(); // ========= this variable
   Rigidbody nrb = nav.GetComponent<Rigidbody>(); // ========== and this variable
   if (ntf.position.x - checkpointOne.position.x > 1 || ntf.position.z - checkpointOne.position.z > 1)
   {
     Debug.Log("Attempting to move character");
     ntf.LookAt(checkpointOne);
     ntf.rotation*=Quaternion.Euler(0f, 90.646f, 15.063f);
     nrb.AddRelativeForce(-100, 0, 0);
     StartCoroutine toBoat(ntf, nrb); // ======== into here
   }
 }

 IEnumerator toBoat(Transform chtf, Rigidbody chrb) // ======== to be used here
 {
   while (ntf.position.x - checkpointOne.position.x > 1 || ntf.position.z - checkpointOne.position.z > 1)
   {
     yield return new WaitForSeconds(0.1f);
   }
 }

where I need to get the transform and rigidbody variables I mentioned with comments into the coroutine to be accessed. Every time I try to compile it just points to the "Start Coroutine" line and says regarding "ntf" and "nrb", "Identifier expected". What am I missing here? Thanks in advance.

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 Bunny83 · Jan 30, 2020 at 02:32 PM

You declared your variables ntf and nrb inside your sendNavaltoBoat method. Those are local variables and only exist inside that method.


However your coroutine toBoat has two parameters. When you start your coroutine you pass (copy) the values of your ntf and nrb variables into those parameters.


Inside your coroutine you have access to those parameters. Those parameters are just like local variables. They only exist inside your coroutine. However you named them differently, namely chtf and chrb. So you probably want to use those inside your coroutine. Of course there is no ntf variable available inside your toBoat coroutine.


Apart from using parameters and local variables, you could have declared the variables in your class instead of creating local variables. Class member variables are accessible to all instance methods. Instance methods are any methods inside a class that are not static and therefore belong to an instance of that class.

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 jonorgan26 · Jan 30, 2020 at 04:23 PM 0
Share

Thanks for getting back to me, I fixed the issues with the variables and now have:

 void getOnShip()
 {
   boat$$anonymous$$ove scr = boatAtBase.GetComponent<boat$$anonymous$$ove>();
   Debug.Log("Get on ship button clicked.");
   boatAtBase.transform.parent = null;
   for (int i = 0; scr.capacity < scr.capacityLimit; i++ )
   {
     StartCoroutine toBoat(i); // ========= This variable gets "Identifier expected"
     scr.upCap();
   }
 }


 IEnumerator toBoat(float navNum)
 {
   Transform ntf = navies[navNum].GetComponent<Transform>();
   Rigidbody nrb = navies[navNum].GetComponent<Rigidbody>();
   ntf.LookAt(checkpointOne);
   ntf.rotation*=Quaternion.Euler(0f, 90.646f, 15.063f);
   Debug.Log("Character turned");
   while (ntf.position.x - checkpointOne.position.x > 1 || ntf.position.z - checkpointOne.position.z > 1)
   {
     Debug.Log("Attempting to move character");
     nrb.AddRelativeForce(-100, 0, 0);
   }
   return null;
 }

But my compiler still points to the mentioned variable and says "Identifier expected", I don't think it sees the "i" I declared two lines above. Any work arounds?

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

253 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

A way to make the player get punished when standing still? 0 Answers

Importing blender model with rigid body 0 Answers

OnTriggerEnter doesn't work if the colliding object is not moving. Translating it by (0,0,0) fixes it. What's the problem? 4 Answers

My Brain is fogged: How do I move a networked object with user input 0 Answers

Vector3.Lerp not working properly, making the player bounce around 2 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