Hello I’m taking a course on building a platformer game and i ran into this problem while working in the Game Data section about making my coins count up when the player collects them
Assets/Scripts/GameCtrl.cs(67,54): error CS1061: ‘string’ does not contain a definition for ‘coinCount’ and no accessible extension method ‘coinCount’ accepting a first argument of type ‘string’ could be found (are you missing a using directive or an assembly reference?)
The Coin Count and Text Coin Count both appear in the inspector of GameCTRL
I’ve bolded the line of GameCtrl code it is referring to… seems like the GameCtrl recognizes the GameData but not coinCount … NEWBIE plz help
Here are my gamedata and gamectrl scripts…
using UnityEngine;
using System.Collections;
using System;
[Serializable]
public class GameData
{
public int coinCount;
}
GAME CTRL
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class GameCtrl : MonoBehaviour
{
public static GameCtrl instance;
public float restartDelay;
public GameData data;
public Text txtCoinCount;
string dataFilePath;
BinaryFormatter bf;
void Awake()
{
if (instance == null)
instance = this;
bf = new BinaryFormatter();
dataFilePath = Application.persistentDataPath + “/petey.dat”;
Debug.Log(dataFilePath);
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
ResetData();
}
public void SaveData()
{
FileStream fs = new FileStream(dataFilePath, FileMode.Create);
bf.Serialize(fs, data);
fs.Close(); //this is important
}
public void LoadData()
{
if (File.Exists(dataFilePath))
{
FileStream fs = new FileStream(dataFilePath, FileMode.Open);
data = (GameData)bf.Deserialize(fs);
// Debug.Log("Number of Coins = " + dataFilePath.coinCount);
txtCoinCount.text = " x " + dataFilePath.coinCount;
fs.Close();
}
}
public void OnEnable()
{
Debug.Log(“Data Loaded”);
LoadData();
}
public void OnDisable()
{
Debug.Log(“Data Saved”);
SaveData();
}
void ResetData()
{
FileStream fs = new FileStream(dataFilePath, FileMode.Create);
//resets coint count
data.coinCount = 0;
bf.Serialize(fs, data);
fs.Close();
Debug.Log(“Data Reset”);
}
///
/// restarts level when player dies
///
public void PlayerDied(GameObject player)
{
player.SetActive(false);
Invoke(“RestartLevel”, restartDelay);
}
public void PlayerDrowned(GameObject player)
{
Invoke(“RestartLevel”, restartDelay);
}
public void UpdateCoinCount()
{
data.coinCount += 1;
txtCoinCount.text = " x " + data.coinCount;
}
void RestartLevel()
{
SceneManager.LoadScene(“Gameplay”);
}
}