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.
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.
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?