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 Octoberwolf · Nov 05, 2018 at 04:16 PM · objectvector3ienumeratorfunction callarguments

How to pass a vector3 as arguement to an IEnumerator in another script?

I have been trying to pass a vector3 into another script but I keep getting different errors. I tried breaking the vector into its x, y, and z components but then I am also getting a null object reference (the script that has the IEnumerator being called is not attached to a game object and does not effect anything it just returns a value). how can I start an IEnumerator in another script with a vector3 as the argument?

Thank you!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Octoberwolf · Nov 05, 2018 at 06:38 PM

My first code is attached to a gameobject and is selected when it is clicked on the code is currently

 public class PawnMovement : MonoBehaviour
 {
     public GameObject Pawn;
     public GameObject GameController;
     public Vector3 PawnPosition;
     public Vector3 MovementCheck;
 
     public Light UnitHighlight;
 
     public PCGrid PCGrid;
 
     void Start ()
     {
         MovementCheck = new Vector3(0, 0, 0);
         Pawn = this.gameObject;
         GameController = GameObject.FindWithTag("GameController");
 
         PawnPosition = Pawn.transform.position;
 
     }
     private void OnMouseDown()
     {
         UnitHighlight.enabled = !UnitHighlight.enabled;
         
         
         if (this.gameObject.tag == "Player")
         {
             MovementCheck = PawnPosition + new Vector3(1, 0, 0);
 
             StartCoroutine(PCGrid.ScanItMoveOnly(MovementCheck.x, MovementCheck.y, MovementCheck.z));
         }            
     }
 }

My other script which contains the coroutine is not attached to any gameobject and is

 public class PCGrid : MonoBehaviour
 {
 
 public IEnumerator ScanItMoveOnly(float ScanItx, float ScanIty, float ScanItz)
     {
         Debug.Log("yes");
     }
 
 }

I currently get an error when I click on the gameobject that says "NullReferenceException: Object reference not set to an instance of an object PawnMovement.OnMouseDown () (at Assets/Scripts/Movements/PawnMovement.cs:52) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)"

cs:52 is the line where I say start coroutine.

Thank you!

Comment
Add comment · Show 4 · 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 swanijam · Nov 06, 2018 at 03:32 PM 1
Share

I believe the Coroutines/IEnumerators are only valid if they contain at least one yield return statement. StartCoroutine is a function that returns an "IEnumerator" and so your function ScanIt$$anonymous$$oveOnly has to return an IEnumerator. The various 'yield return' functions satisfy that condition.

Try adding a line "yield return null" right after debug.Log("yes")

avatar image Octoberwolf · Nov 07, 2018 at 01:36 PM 0
Share

I added in the yield return null, but I still get the same error

"NullReferenceException: Object reference not set to an instance of an object Pawn$$anonymous$$ovement.On$$anonymous$$ouseDown () (at Assets/Scripts/$$anonymous$$ovements/Pawn$$anonymous$$ovement.cs:52) UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32)"

How do I call the script with a reference to an object?

avatar image Casiell Octoberwolf · Nov 07, 2018 at 02:16 PM 1
Share

So you have a null reference exception. You have to check what objects you are referencing in that line (line 52) and make sure that you assigned them.

Looking at this code the most likely culprit is PCGrid. It is a public variable and you don't assign it in code, so you have to drag it in the editor.

avatar image Octoberwolf Casiell · Nov 07, 2018 at 03:58 PM 0
Share

I assigned it in the editor and it worked! Thank you!

avatar image
1

Answer by swanijam · Nov 05, 2018 at 04:20 PM

Could you share the code you're using to try to trigger it? you should be able to pass any kind of arguments to an IEnumerator to start a coroutine with it, so it's very likely that you're calling it incorrectly altogether.

Comment
Add comment · 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

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

123 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

Related Questions

overlapping object doesn't disable 1 Answer

How place object in forward direction of the camera, even if camera is pointing to the ground or the sky? 2 Answers

Object shows up on canvas until moved then disappears despite other objects at same position showing up? 0 Answers

How to get Raycast to detect collision with Object? 3 Answers

Moving an object 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