I’m stuck on this little hang-up, and I can’t figure out why Unity won’t compile this script, except for the fact that Visual Studio seems to expect me to end my start() function after the first line. I’m just starting to learn C#, so any help/advice/criticism would be much appreciated!
Visual Studio underlines the semi-colon following “float movementSpeed = 1” and says “} expected” and underlines the word void in front of update() and says “A namespace cannot directly contain members such as fields or methods.”
using UnityEngine;
using System.Collections;
public class SoldierControl : MonoBehaviour
{
void start()
{
float movementSpeed = 1;
public float jumpHeight = 1;
public int lowerLimit = -10;
int health = 100;
int yPos = transform.position.y;
bool isGrounded = false;
}
void update()
{
if (Input.GetAxis("Horizontal") == 1)
moveRight();
if (Input.GetAxis("Horizontal") == -1)
moveLeft();
if (Input.GetAxis("Jump") == 1 && isGrounded)
jump();
if (yPos < lowerLimit || health < 1)
kill();
}
void moveRight()
{
rigidbody.AddForce(Vector3.right * movementSpeed);
}
void moveLeft()
{
rigidbody.addForce(Vector3.left * movementSpeed);
}
void jump()
{
rigidbody.addForce(Vector3.up * jumpHeight);
}
void kill()
{
destroy();
}
}