- Home /
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!
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!
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")
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?
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.
I assigned it in the editor and it worked! Thank you!
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.
Your answer
Follow this Question
Related Questions
overlapping object doesn't disable 1 Answer
How to get Raycast to detect collision with Object? 3 Answers
Moving an object 1 Answer