Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

changing the alpha of a game object by clicking on another gameobject

Discussion in 'Getting Started' started by luffyy-Sarah48, Jun 7, 2019.

  1. luffyy-Sarah48

    luffyy-Sarah48

    Joined:
    May 10, 2019
    Posts:
    3
    AM new TO UNITY and maybe it sounds lame but i can't found the solution so please help me



    i want a c# script where i can change the alpha of a gameobject by clciking on another gameobject ?




    i tried this script which is attached to the clicked object and ofcourse it didin't work .


    using UnityEngine;
    using System.Collections;
    using UnityEngine.SceneManagement;

    public class close : MonoBehaviour {

    GameObject s;



    private void OnMouseDown()
    {
    s.GetComponent.renderer.material.color = Color(1.0f, 1.0f, 1.0f, 0.0f);

    }
    }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,261
    Well, you're off to a decent start. A few tips:

    1. When posting code, use the "Code:" button so it's formatted nicely.
    upload_2019-6-7_7-2-37.png
    2. Use Debug.Log to figure out what's going on. In this case, put one in your OnMouseDown event, so you can see if that's getting called at all. If not, forget about changing alpha for a while, and focus on detecting a mouse click on your object.
    3. If your method is getting called, then I bet it's generating an error (because s is nil). Always check for errors, and paste them into your forum post if you can't figure them out.
    4. In order for s to not be null, you will either need some more code that assigns it a value (maybe using GameObject.Find), or you will need to make it public, and assign it a value in the inspector, like in those beginner tutorials I'm sure you went through. ;)