Home>

With Unity2D (version 2017 1.0f3),
I want to change the transparency of only the object at the clicked position.

Error message

All SpriteRenderer objects registered in Prefab are reflected.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StageManager: MonoBehaviour {
    public GameObject block;
    public SpriteRenderer spRenderBlock;
    // Use this for initialization
    void Start () {
        this.block = GetComponent<GameObject>();
        this.spRenderBlock = GetComponent<SpriteRenderer>();
    }
    // Update is called once per frame
    void FixedUpdate () {
        if (Input.GetMouseButtonDown (0))
        {
            OnClickBlock ();
        }
    }
    // mouse click or tap
    public void OnClickBlock () {
        // Get the coordinates of the mouse as seen from the screen
        Vector2 tapPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
        // Object with collider = coordinates of clicked location
        Collider2D collider = Physics2D.OverlapPoint (tapPoint);
        // Process only when the collider of Block is acquired
        if (collider! = null&&collider.name.Equals ("Block")) {
            // This block gets the value as the collider's GameObject
            this.block = collider.transform.gameObject;
            SwitchBlockDisplay (collider);
        }
    }
    // Processing to switch display state
    private void SwitchBlockDisplay (Collider2D blockCollider) {
        this.spRenderBlock.color = new Color (1f, 1f, 1f, 0.3f);
        blockCollider.isTrigger = true;
    }
  • Answer # 1

    To see the code, is StageManager a script attached to each block?

    Imagine the process on that assumption, when you click on a block,Input.GetMouseButtonDown (0)istrueregardless of the position of the mouse It is expected that all blocks in the scene will executeOnClickBlock ();all at once.

    And processing continues toSwitchBlockDisplay (collider);, wherethis.spRenderBlock.color = new Color (1f, 1f, 1f, 0.3f);changes your own color to translucent.
    No matter which block you click, all the blocks will reach this color change process, so all the blocks will become semi-transparent all at once (I'm sorry for the complicated explanation ...).

    What about changing the conditions a bit and doing this?

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class StageManager: MonoBehaviour {
        public GameObject block;
        public SpriteRenderer spRenderBlock;
        // Use this for initialization
        void Start () {
            // GameObject is not "Component"
            // block doesn't seem to be used in particular, but if you want to use it, you can get it with "this.gameObject"
            this.block = GetComponent<GameObject>();
            // Debug.Log (this.block);// I think that it is "null" when outputting to the console
            this.spRenderBlock = GetComponent<SpriteRenderer>();
        }
        // Update is called once per frame
        // Is there any special reason for using "FixedUpdate"?
        // It may be better to leave this as "Update"
        // With FixedUpdate, OnClickBlock may or may not be called multiple times
        // Reference 1 https://forum.unity.com/threads/input-getmousebuttondown-and-fixedupdate.130051/
        // Reference 2 http://unity-yuji.xyz/input-fixedupdate/
        void FixedUpdate () {
            if (Input.GetMouseButtonDown (0)) {
                OnClickBlock ();
            }
        }
        // mouse click or tap
        public void OnClickBlock () {
            // Get the coordinates of the mouse as seen from the screen
            Vector2 tapPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
            // Object with collider = coordinates of clicked location
            Collider2D collider = Physics2D.OverlapPoint (tapPoint);
            // Process only when the collider of Block is acquired
            // Partially change the condition ... not "Is the object name Block"
            // "Does the acquired collider's gameObject match the object to which this script is attached?"
            if (collider! = null&&collider.gameObject == this.gameObject) {
                // This block gets the value as the collider's GameObject
                this.block = collider.transform.gameObject;
                SwitchBlockDisplay (collider);
            }
        }
        // Processing to switch display state
        private void SwitchBlockDisplay (Collider2D blockCollider) {
            this.spRenderBlock.color = new Color (1f, 1f, 1f, 0.3f);
            blockCollider.isTrigger = true;
        }
    }