Witam,
Od kilku dni staram się przyswoić podstawowe funkcje języka c sharp, pod Unity3D.
Jestem na etapie pisania skryptu, który respawn'uje postać po kolizji z przeciwnikiem.
void OnTriggerEnter(Collider innyObiekt)
{
if(innyObiekt.tag == "enemy")
{
StartCoroutine(niszczenieGracza());
Destroy(innyObiekt.gameObject);
}
}
IEnumerable niszczenieGracza()
{
gameObject.renderer.enable = false;
transform.position = new Vector3(-10, transform.position.y, transform.position.z);
yield return new WaitForSeconds(0.5f);
gameObject.renderer.enabled = true;
}
Wystąpiło kilka errorów przy debug'owaniu:
CS1503 C# Argument 1: cannot convert from 'System.Collections.IEnumerable' to 'System.Collections.IEnumerator'
CS0619 C# 'GameObject.renderer' is obsolete: 'Property renderer has been deprecated. Use GetComponent<Renderer>() instead. (UnityUpgradable)'
CS0619 C# 'GameObject.renderer' is obsolete: 'Property renderer has been deprecated. Use GetComponent<Renderer>() instead. (UnityUpgradable)'
CS1061 C#does not contain a definition for
and no extension methodaccepting a first argument of type
could be found (are you missing a using directive or an assembly reference?)
CS1061 C#does not contain a definition for
and no extension methodaccepting a first argument of type
could be found (are you missing a using directive or an assembly reference?)
Uporałem się trochę z problemem, ale okrężną drogą i niezbyt skuteczną. Tak przekształciłem kod:
void OnTriggerEnter(Collider innyObiekt)
{
if(innyObiekt.tag == "enemy")
{
StartCoroutine(NiszczenieGracza());
Destroy(innyObiekt.gameObject);
}
}
IEnumerable NiszczenieGracza()
{
gameObject.GetComponent<Renderer>(enabled) = false;
transform.position = new Vector3(-10, transform.position.y, transform.position.z);
yield return new WaitForSeconds(0.5f);
gameObject.GetComponent<Renderer>(enabled) = true;
}
Jestem strasznie zielony w tym temacie, ale każdy od czegoś zaczynał. ;)
Wszelką krytykę przyjmę na klatę, a za pomoc i objaśnienie (mile widziane) szczerze dziękuję. :)