- Home /
Question by
UnityNoob123 · Feb 08, 2017 at 06:10 AM ·
passing
How to pass vars into functions
Hello, I am trying to pass a variable into a function but I am not sure what the variable should be. If you reference my code I am trying to pass the var movement variable to my IEnumerator slowMoveTime function. Thanks for the help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class slowMovement : MonoBehaviour {
public int damage;
public float time;
public float speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter (Collider col) {
if (col.GetComponent<Collider>().tag == "LocalPlayer") {
Debug.Log ("hit the remoteplayer");
var hit = col.gameObject;
var health = hit.GetComponent<Health> ();
var movement = hit.GetComponent<PlayerController> ();
if (health != null) {
health.TakeDamage (damage);
}
if (movement != null) {
movement.speed = speed;
StartCoroutine(slowMoveTime (time, movement));
}
Destroy (gameObject);
}
}
IEnumerator slowMoveTime (float time,"what goes here" movement) {
yield return new WaitForSeconds (time);
}
}
Comment
Answer by HarshadK · Feb 08, 2017 at 06:28 AM
Based on theline:
var movement = hit.GetComponent<PlayerController> ();
your movement variable is of type PlayerController, which is the type you need to set for the coroutine. So your coroutine definition becomes:
IEnumerator slowMoveTime (float time, PlayerController movement) {