Is my method just as good?

I ended up pausing right at the beginning and did everything by myself and it worked exactly as intended!

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

public class DefenderSpawner : MonoBehaviour
{
     Defender defenderPrefab;
     int defenderPrice;
     int rupeeWallet;
   //Delcared vars??
   // Vector2 worldPosition;
   //Debug controls

   //make these in their own catagory called dogeDebugParams
   //I CANT REMEMBERRRR
   [SerializeField] bool dogeIsPermaPos;
    [SerializeField] Vector2 dogePermaPosition;

    // Start is called before the first frame update
  

    // Update is called once per frame
   void OnMouseDown()
    {
        //MousePositioner();
        SpawnDefender(MousePositioner());
        rupeeWallet = FindObjectOfType<RupeeCounter>().rupees;

    }

    public void SetSelectedDefender(Defender defenderToSelect, int priceOfRupees)
    {
        defenderPrefab = defenderToSelect;
        defenderPrice = priceOfRupees;
        
    }


    Vector2 MousePositioner()
    {
        Vector2 mousePos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
        Vector2 worldPosition = Camera.main.ScreenToWorldPoint(mousePos);
        Vector2 gridPos = SnapToGrid(worldPosition);
        return gridPos;
       
        //worldPosition = Camera.main.ScreenToWorldPoint(mousePos);


    }
    private Vector2 SnapToGrid (Vector2 rawWorldPos)
    {
        float newX = Mathf.RoundToInt(rawWorldPos.x);
        float newY = Mathf.RoundToInt(rawWorldPos.y);
        return new Vector2(newX, newY);

    }
    void SpawnDefender(Vector2 worldPosition)
    {
        

        //GameObject newDefender = Instantiate (defenderPrefab, worldPosition, Quaternion.identity) as GameObject;
        if (rupeeWallet >= defenderPrice)
        {
            Defender newDefender = Instantiate (defenderPrefab, worldPosition, Quaternion.identity) as Defender;
            FindObjectOfType<RupeeCounter>().SpendRupees(defenderPrice);
            Debug.Log("Defender Placed");
            
            // Instantiate(defenderPrefab, worldPosition, Quaternion.identity);
        }
        else if(defenderPrice >= rupeeWallet)
        {
            Debug.Log("NotEnoughRupees");
        }
       


    }
    
}

But when I saw Rick do it I see that he has cached a reference to the counter itself and made a return bool method that tells it whether or not the cost enough. I did sort of the same thing but more so on the fly and I had DefenderSpawner.cs do most of the work.

Is this worse or less efficient? should I change it to match Ricks or will I be fine moving on?

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

public class DefenderButton : MonoBehaviour
{
    [SerializeField] Defender defenderObject;
    
    [SerializeField] int starValue = 20;
    // Start is called before the first frame update
    private void OnMouseDown()
    {
        


        var buttons = FindObjectsOfType<DefenderButton>();
        foreach(DefenderButton button in buttons) 
        {
            button.GetComponent<SpriteRenderer>().color = new Color32(41,41,41,255);
        }
        GetComponent<SpriteRenderer>().color = Color.white;

        FindObjectOfType<DefenderSpawner>().SetSelectedDefender(defenderObject, starValue);



       
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

this is DefenderButton.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RupeeCounter : MonoBehaviour
{
    [SerializeField] public int rupees = 60;
    Text rupeeText;

    private void Start()
    {
        rupeeText = GetComponent<Text>();
        UpdateDisplay();
    }
    private void Update()
    {
        
    }
    private void UpdateDisplay()
    {
        rupeeText.text = rupees.ToString();
    }
    public void AddStars(int amount)
    {
        rupees += amount;
        UpdateDisplay();
    }

    public void SpendRupees(int amount)
    {
        if (rupees < amount)
        {
            rupees = 0;
        }
        else
        {
            rupees -= amount;
            UpdateDisplay();
        }
    }
}

And this is my version of Starcounter known as RupeeCounter.cs

Hi Xarce,

First of all, good job on challenging yourself. :slight_smile:

If everything works as intended, you obviously developed a solution. It does not matter if Rick’s code is different because there are often multiple ways to solve a problem in Unity. Furthermore, some things are a matter of personal preference, e.g. the decision whether the cost should be part of the DefenderSpawner or not.

Just one thing that I noticed immediately: the empty Update methods. To improve the performance of your code, remove all empty methods.

Keep up the good work! :slight_smile:


See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms