Home Game Development unity – How can I modify the feel of a prefab clone when it is clicked?

unity – How can I modify the feel of a prefab clone when it is clicked?

0
unity – How can I modify the feel of a prefab clone when it is clicked?

[ad_1]

I’ve a prefab (referred to as ArableLand), and after I click on on the bottom within the play mode, the ArableLand prefab instantiates a clone of itself.

For instance, if I click on 3 occasions on 3 completely different locations, then 3 clones of the ArableLand prefab will probably be instantiated.

I’ve 2 Boolean variables:

To go along with this, I even have 2 sprites:

  • PlantedCarrotSprite
  • PlantedBananaSprite

When I click on on an ArableLand occasion, and I’ve for instance PlantCarrot set to true, I would like this occasion’s texture to show into PlantedCarrotSprite.

If I’ve greater than 1 clone of this prefab, then I wish to change the clicked clone’s texture, not every other (not less than till I click on the opposite one as nicely).

How can I do that?

Here is the script I presently use on the ArableLand prefab:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class FarmingSystem : MonoBehaviour
{
    //the plant panel is made out of buttons which include the booleans I discussed earlier than
    public GameObject PlantPanel;
    //it is a script within the PlantPanel
    public Plant plantScr;

    //Sprites of planted meals
    public Sprite PlantedCarrotSprite;
    public Sprite PlantedBananaSprite;

    //Check if plant is planted
    public bool IsCarrotPlanted;
    public bool IsBananaPlanted;

    public void Start()
    {
        PlantPanel = GameObject.Find("Canvas/PlantPanelObj/PlantPanel");

        plantScr = GameObject.Find("Canvas/PlantPanelObj/PlantPanel").GetComponent<Plant>();;
    }

    public void OnMouseDown()
    {
        PlantPanel.SetActive(true);

        if (plantScr.PlantButtonNumber == 1 && plantScr.Carrot != null)
        {
            PlantCarrotFarm();
        }
        
        if (plantScr.PlantButtonNumber == 2 && plantScr.Banana != null)
        {
            PlantBananaFarm();
        }
    }

    public void PlantCarrotFarm()
    {
        this.gameObject.GetComponent<SpriteRenderer>().sprite = PlantedCarrotSprite;
        IsCarrotPlanted = true;
        Destroy(plantScr.Carrot.gameObject);
    }

    public void PlantBananaFarm()
    {
        this.gameObject.GetComponent<SpriteRenderer>().sprite = PlantedBananaSprite;
        IsBananaPlanted = true;
        Destroy(plantScr.Banana.gameObject);
    }
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here