C# Int Problem

Hello, I am new to C#, I got intermediate in C++ so I know a few things, but I am stuck on this one thing,

I am doing the roll-a-ball project to get a grips with Unity and everything and things were going smoothly, but in the 4.6 update they changed the gui system so part of the tutorial is now irrelevant. It’s the counting gui, I noticed this so I tried to improvise but being the beginner I am it didn’t go so well.

Basically I have this

public class PlayerController : MonoBehaviour 
{
	public string stringToEdit = "Collected : " + count.ToString;
	void OnGUI() {
	stringToEdit = GUI.TextField(new Rect(10, 10, 200, 20), stringToEdit, 25);
	}
	public float speed;

	void Start ()
	{
		int count;
		count = 0;
	}

for displaying the gui but I have to put my count INT in void Start () so it does not recognize it.

Here’s the whole code (some of the code is for moving the ball ):

using UnityEngine;
using System.Collections;


public class PlayerController : MonoBehaviour 
{
	public string stringToEdit = "Collected : " + count.ToString;
	void OnGUI() {
	stringToEdit = GUI.TextField(new Rect(10, 10, 200, 20), stringToEdit, 25);
	}
	public float speed;

	void Start ()
	{
		int count;
		count = 0;
	} 


	void FixedUpdate () {

		float moveHorizontal = Input.GetAxis ("Horizontal");
		float moveVertical = Input.GetAxis ("Vertical");
		Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

		rigidbody.AddForce (movement * speed * Time.deltaTime);
	}
	void OnTriggerEnter(Collider other) 
	{	
		if (other.gameObject.tag == "PickUp") {
			other.gameObject.SetActive (false);
			count = count + 1;	}
	}
}

A way to get this working would be much appreciated, this is probably very obvious to alot of you.


public float speed;
private int count;

void Start ()
{         
    count = 0;
}
...

In your code, count is declared in the scope of Start() method. You want count to be in scope of PlayerController class. Then everything under its scope ( Start() , FixedUpdate() , OnGUI() can access it).