Friday, October 7, 2022
HomeGame Developmentunity - How can I modify the feel of a prefab clone...

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


I’ve a prefab (known 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 instances on 3 totally 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

After 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 need to change the clicked clone’s texture, not another (a minimum of till I click on the opposite one as properly).

How can I do that?

Right here is the script I at the moment 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;
    //this can be a script within the PlantPanel
    public Plant plantScr;

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

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

    public void Begin()
    {
        PlantPanel = GameObject.Discover("Canvas/PlantPanelObj/PlantPanel");

        plantScr = GameObject.Discover("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);
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments