- Home /
Question by
justadeveloper · Nov 23, 2015 at 12:58 PM ·
spritespriterendererienumeratorfadecasting
How can i get my gameobject as a sprite from its collider name?
i want to apply fade effect on my gameobject(in this case,the game object is sprite) only if i touched the object. Im confused how to cast my collider.name to gameobject(sprite).....I can detect the collider name but i cant apply fade effect on them because the parameter in my IEnumerator function is Spriterenderer....please help me
using UnityEngine; using System.Collections;
public class Touch : MonoBehaviour { public SpriteRenderer sprite;
//Update is called once per frame
void Update ()
{
if (Application.platform == RuntimePlatform.WindowsEditor) // Else if Desktop
{
if (Input.GetMouseButtonDown (0))
{
//StartCoroutine(Fade(/*what should i write here*/));
rayCol();
}
}
}
private void rayCol()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if(Input.GetMouseButtonDown(0))
{
if(Physics.Raycast(ray, out hit))
{
//what should i write here
Debug.Log(hit.);
}
}
}
IEnumerator Fade(SpriteRenderer spriteName)
{
for (float f = 1f; f > -0.01f; f -= 0.1f)
{
Color c = spriteName.material.color;
c.a = f;
spriteName.material.color = c;
yield return null;
}
}
}
Comment
Best Answer
Answer by meat5000 · Nov 23, 2015 at 03:49 PM
Use GetComponent
http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html