Help me fix my code!!
After MonoDevelop giving up and killing itself 30 mins after I got it working by a complete reinstall I gave up and decided to deal with Visual Studio's cluttered and utterly disgusting interface so I could code without errors but now half my code is underlined in red that wasn't a week ago and ugh I don't even know.
I have an unexpected symbol 'void' at line 37 but was at line 46 (magically went away... see what I mean) and Visual Studio doesn't recognize the update part in the void. It does however in my other rotation script. (once again see why I am so confused)
Anyway here is a copy of my script and any fixes would be helpful. (I'm very much a beginner at programming so excuse my bad scripting techniques)
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
public Keycode ResetKey = Keycode.r;
public string SceneName = "MiniGame";
private Rigidbody rb;
private int count;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
winText.text = "";
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
}
void Update () {
if (Input.GetKeyDown("r"))
{
Application.LoadLevel(MiniGame);
}
} void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag("Pick Up")) { other.gameObject.SetActive(false); count = count + 1; SetCountText(); } }
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 24)
{
winText.text = "You Win!";
}
}
}
Answer by SteenPetersen · Aug 07, 2017 at 07:54 AM
You have an extra "}"
use this:
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
public Keycode ResetKey = Keycode.r;
public string SceneName = "MiniGame";
private Rigidbody rb;
private int count;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
void Update()
{
if (Input.GetKeyDown("r"))
{
Application.LoadLevel(MiniGame);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false); count = count + 1; SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 24)
{
winText.text = "You Win!";
}
}
}
Should work.
Thank you so much for fixing 90% of it that was a $$anonymous$$AJOR help!!
Everything is fixed except this, any idea?
ERROR: The type or namespace name 'keycode' could not be found. are you missing assembly reference? - line 11
If you can fix this your a god and I love you!!
remove this line from your variables:
public $$anonymous$$eycode Reset$$anonymous$$ey = $$anonymous$$eycode.r;
hope that helps, remember to mark answer as correct if it does.
Fixed the keycode thing but now there's this...
It is saying: The name '$$anonymous$$iniGame' does not exist within the current context.
even though it is referenced in the variables??
line 33 in the code below...
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour {
public float speed;
public Text countText;
public Text winText;
public string SceneName = "$$anonymous$$iniGame";
private Rigidbody rb;
private int count;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
void Update()
{
if (Input.Get$$anonymous$$eyDown("r"))
{
Application.LoadLevel($$anonymous$$iniGame); - this is the problem line $$anonymous$$inigame is underlined -
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false); count = count + 1; SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 24)
{
winText.text = "You Win!";
}
}
}