So I wrote this script based off of a script that was used in an answer on this website, but I have a problem: Unity says that public is unexpected and it was expecting the EOF. I’m not sure how to fix this since I’m rather new. This is a script for a shooting gun, and I need the projectile to be public, however the word “public” 1;1 is not recognized in the system. Please help, thank you!
public( Rigidbody )projectile;
public float speed = 20;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
Rigidbody instantiatedProjectile = Instantiate(projectile,
transform.position,
transform.rotation)
as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
}
}
Without the parentheses, I have these two errors: Unexpected token: rigidbody Unexpected token: float ';' expected. Add a semicolon. I added to parentheses to take those away, however I'm not sure why it doesn't recognize the 'public'.
– JoebearSounds like you either created a javascript file but copied c# code, or you created a c# script but forgot to create the class definition. What type of script are you going for?
– pekaliciousJavascript and c# have wildly different syntax. Not to mention that you cannot easily have a javascript class talk to a c# class and vise versa. Here is a short video explaining the differences: https://unity3d.com/learn/tutorials/modules/beginner/scripting/c-sharp-vs-javascript-syntax I suggest choosing one type of script and sticking with it. My recommendation would be c#, but choose whatever you find easier.
– pekaliciouswell, place it in the scene. Add a sphere collider to it. In a script on the lever you can check for input in OnCollisionStay, if the staying object is the player. if you pressed a button, call SetBool or something like that on the Animator component to change its state. That's a basic approach, but will work, but for it to be realised, I think you'll have to dig into scripting and the Unity docs yourself. Otherwise you'll just get this running and won't be able to get very far on your own.
– hexagonius