Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How can i get my gameobject(sprite)with raycast and then use fade effect on raycasted object?

Discussion in 'Scripting' started by justadeveloper, Nov 23, 2015.

  1. justadeveloper

    justadeveloper

    Joined:
    Jan 12, 2014
    Posts:
    33
    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*/));
    }
    }
    }

    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,I want to get my object with my raycast,I can raycast colider.name but i dont know how to get my gameobject(In SpriteRendered form)
    }
    }

    }

    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;
    }
    }
    }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. justadeveloper

    justadeveloper

    Joined:
    Jan 12, 2014
    Posts:
    33