Skip to main content
Minor grammar fixes, code formatting
Source Link
liggiorgio
  • 5.5k
  • 6
  • 27
  • 38

Unity 2D Script effectsaffects all current objects in a Scene

I Lookedlooked around for a fix because I am sure it's either super simple of a fix or something that is commonly ranrun across but non the lessnonetheless it's about time I asked straight up.

This is my Codecode in the script. ItsIt's growing plants based on time but I'm mostly just Trying out Time based events and ran into a bit of a problem. When I click on one plant, the other 2 get harvested. Here is the code I ended up with:

========================

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

public class betterPlantGrowthController : MonoBehaviour
{
    // This is a small location where to keep all pre-read and mostly prm Varables such as Time or Public Varables.
    // ======================Varables=============================

    public SpriteRenderer spriteRenderer;
    public Sprite[] spriteArray;
    public float time_ = 0;
    public double growthTime = 0;

    private int currentArrayNumber = 0;
    private bool currentlyGrowing = true;
 

    // ===========================================================
 

    void Update() // This updates every frame of the game/programe and will be where most your code ends up
    {
 
        if (currentlyGrowing == true)
        {
            time_ += Time.deltaTime; // Updates ever Sec

            if (time_ >= growthTime)
            {
                time_ = 0;
                currentArrayNumber += 1;
            }

            spriteRenderer.sprite = spriteArray[currentArrayNumber]; // Renders The Current Sprite/Plant-stage

            if (currentArrayNumber == 4) { currentlyGrowing = false; }
        }

        if (currentlyGrowing == false)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);

                RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);

                if (hit.collider != null)
                {
                    currentArrayNumber = 0;
                    spriteRenderer.sprite = spriteArray[currentArrayNumber]; // Reseting the Render
                    currentlyGrowing = true;
                }
            }
        }
 
    }
}

======================

It's a super simple wait till it grows and then clickclicks on it. Again, when they all reach stage 4 (the last stage) then click on it, they all revert to stage 0 (fristfirst stage) even tho there are multiple objects running in the game. Any ideas? Or suggestions on how to make it so that only the one clicked is updated/changed?

Unity 2D Script effects all current objects in a Scene

I Looked around for a fix because I am sure it's either super simple of a fix or something that is commonly ran across but non the less it's about time I asked straight up.

This is my Code in the script. Its growing plants based on time but I'm mostly just Trying out Time based events and ran into a bit of a problem. When I click on one plant, the other 2 get harvested. Here is the code I ended up with:

========================

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

public class betterPlantGrowthController : MonoBehaviour
{
    // This is a small location where to keep all pre-read and mostly prm Varables such as Time or Public Varables.
    // ======================Varables=============================

    public SpriteRenderer spriteRenderer;
    public Sprite[] spriteArray;
    public float time_ = 0;
    public double growthTime = 0;

    private int currentArrayNumber = 0;
    private bool currentlyGrowing = true;
 

    // ===========================================================
 

    void Update() // This updates every frame of the game/programe and will be where most your code ends up
    {
 
        if (currentlyGrowing == true)
        {
            time_ += Time.deltaTime; // Updates ever Sec

            if (time_ >= growthTime)
            {
                time_ = 0;
                currentArrayNumber += 1;
            }

            spriteRenderer.sprite = spriteArray[currentArrayNumber]; // Renders The Current Sprite/Plant-stage

            if (currentArrayNumber == 4) { currentlyGrowing = false; }
        }

        if (currentlyGrowing == false)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);

                RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);

                if (hit.collider != null)
                {
                    currentArrayNumber = 0;
                    spriteRenderer.sprite = spriteArray[currentArrayNumber]; // Reseting the Render
                    currentlyGrowing = true;
                }
            }
        }
 
    }
}

======================

It's a super simple wait till it grows and then click on it. Again, when they all reach stage 4 (the last stage) then click on it, they all revert to stage 0 (frist stage) even tho there are multiple objects running in the game. Any ideas? Or suggestions on how to make it so that only the one clicked is updated/changed?

Unity 2D Script affects all current objects in a Scene

I looked around for a fix because I am sure it's either super simple of a fix or something that is commonly run across but nonetheless it's about time I asked straight up.

This is my code in the script. It's growing plants based on time but I'm mostly just Trying out Time based events and ran into a bit of a problem. When I click on one plant, the other 2 get harvested. Here is the code I ended up with:

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

public class betterPlantGrowthController : MonoBehaviour
{
    // This is a small location where to keep all pre-read and mostly prm Varables such as Time or Public Varables.
    // ======================Varables=============================

    public SpriteRenderer spriteRenderer;
    public Sprite[] spriteArray;
    public float time_ = 0;
    public double growthTime = 0;

    private int currentArrayNumber = 0;
    private bool currentlyGrowing = true;

    // ===========================================================

    void Update() // This updates every frame of the game/programe and will be where most your code ends up
    {
        if (currentlyGrowing == true)
        {
            time_ += Time.deltaTime; // Updates ever Sec

            if (time_ >= growthTime)
            {
                time_ = 0;
                currentArrayNumber += 1;
            }

            spriteRenderer.sprite = spriteArray[currentArrayNumber]; // Renders The Current Sprite/Plant-stage

            if (currentArrayNumber == 4) { currentlyGrowing = false; }
        }

        if (currentlyGrowing == false)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);

                RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);

                if (hit.collider != null)
                {
                    currentArrayNumber = 0;
                    spriteRenderer.sprite = spriteArray[currentArrayNumber]; // Reseting the Render
                    currentlyGrowing = true;
                }
            }
        }
    }
}

It's a super simple wait till it grows and then clicks on it. Again, when they all reach stage 4 (the last stage) then click on it, they all revert to stage 0 (first stage) even tho there are multiple objects running in the game. Any ideas? Or suggestions on how to make it so that only the one clicked is updated/changed?

Source Link

Unity 2D Script effects all current objects in a Scene

I Looked around for a fix because I am sure it's either super simple of a fix or something that is commonly ran across but non the less it's about time I asked straight up.

This is my Code in the script. Its growing plants based on time but I'm mostly just Trying out Time based events and ran into a bit of a problem. When I click on one plant, the other 2 get harvested. Here is the code I ended up with:

========================

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

public class betterPlantGrowthController : MonoBehaviour
{
    // This is a small location where to keep all pre-read and mostly prm Varables such as Time or Public Varables.
    // ======================Varables=============================

    public SpriteRenderer spriteRenderer;
    public Sprite[] spriteArray;
    public float time_ = 0;
    public double growthTime = 0;

    private int currentArrayNumber = 0;
    private bool currentlyGrowing = true;


    // ===========================================================


    void Update() // This updates every frame of the game/programe and will be where most your code ends up
    {

        if (currentlyGrowing == true)
        {
            time_ += Time.deltaTime; // Updates ever Sec

            if (time_ >= growthTime)
            {
                time_ = 0;
                currentArrayNumber += 1;
            }

            spriteRenderer.sprite = spriteArray[currentArrayNumber]; // Renders The Current Sprite/Plant-stage

            if (currentArrayNumber == 4) { currentlyGrowing = false; }
        }

        if (currentlyGrowing == false)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);

                RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);

                if (hit.collider != null)
                {
                    currentArrayNumber = 0;
                    spriteRenderer.sprite = spriteArray[currentArrayNumber]; // Reseting the Render
                    currentlyGrowing = true;
                }
            }
        }

    }
}

======================

It's a super simple wait till it grows and then click on it. Again, when they all reach stage 4 (the last stage) then click on it, they all revert to stage 0 (frist stage) even tho there are multiple objects running in the game. Any ideas? Or suggestions on how to make it so that only the one clicked is updated/changed?