Getting error just in unity not Visual studio

Hi

I am getting compile error just unity not in Visual Studio. I cannot see why this is happening the error says that "Defender does not contain a definition for ‘GetStarCost’ and no extion method 'GetStarCost? of type ‘Defender’ could be found. . Here is the code

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

public class Defender : MonoBehaviour {

[SerializeField] int starCost = 100;

public int GetStarCost()
{
    return starCost;
}

public void AddStars (int amount)
{
    FindObjectOfType<StarDisplay>().AddStars(amount);
}

}

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

public class StarDisplay : MonoBehaviour {
[SerializeField] int stars = 100;
Text starText;

void Start ()
{
    starText = GetComponent<Text>();
    UpdateDisplay();
}
private void UpdateDisplay()
{
    starText.text = stars.ToString();
}

public bool HaveEnoughStars(int amount)
{
    return stars >= amount;
}

public void AddStars(int amount)
{
    stars += amount;
    UpdateDisplay();
}
public void SpendStars(int amount)
{
    if (stars >=amount)
    { 
    stars -= amount;
    UpdateDisplay();
    }
}

}

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

public class DefenderSpawner : MonoBehaviour {

//[SerializeField] Defender;
Defender defender;

private void OnMouseDown()
{
    AttemptToPlaceDefenderAt(GetSquareClicked());
}

public void SetSelectedDefender (Defender defenderToSelect)
{
    defender = defenderToSelect;
}
private void AttemptToPlaceDefenderAt (Vector2 gridPos)
{
    var StarDisplay = FindObjectOfType<StarDisplay>();
    int defenderCost = defender.GetStarCost();
    if (StarDisplay.HaveEnoughStars(defenderCost))
    {
        SpawndDefender(gridPos);
        StarDisplay.SpendStars(defenderCost);
    }

}
private Vector2 GetSquareClicked()
{
    Vector2 clickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    Vector2 worldPos = Camera.main.ScreenToWorldPoint(clickPos);
    Vector2 gridPos = SnapToGrid(worldPos);
    return gridPos;
}
private Vector2  SnapToGrid(Vector2 rawWorldPos)
{
    float newX = Mathf.RoundToInt(rawWorldPos.x);
    float newY = Mathf.RoundToInt(rawWorldPos.y);
    return new Vector2(newX, newY);
}
private void SpawndDefender(Vector2 roundedPos)
{
    Defender newDefender = Instantiate(defender, roundedPos, Quaternion.identity) as Defender;
    Debug.Log(roundedPos);
}

}

Best Regards
Gummiunity 2d

Hi me again sorry for being clumsy I found out the Error I forgot to save the Defender script.

2 Likes

Privacy & Terms