- Home /
When I try to copy and paste something to my script... I get an error.
For some reason when I try and copy a small snippet that WILL work with my current playerscript, it goves me an error. Here's my current player script.
using UnityEngine;
/// /// Player controller and behavior /// public class PlayerScript : MonoBehaviour { ///
/// 1 - The speed of the ship /// public Vector2 speed = new Vector2(50, 50); // 2 - Store the movement
private Vector2 movement;
void Update()
{
// 3 - Retrieve axis information
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
// 4 - Movement per direction
movement = new Vector2(
speed.x * inputX,
speed.y * inputY);
}
void FixedUpdate()
{
// 5 - Move the game object
rigidbody2D.velocity = movement;
}
}
here's what I want to add to the script, but it keep giving me an error
void Update() { // ...
// 5 - Shooting
bool shoot = Input.GetButtonDown("Fire1");
shoot |= Input.GetButtonDown("Fire2");
// Careful: For Mac users, ctrl + arrow is a bad idea
if (shoot)
{
WeaponScript weapon = GetComponent<WeaponScript>();
if (weapon != null)
{
// false because the player is not an enemy
weapon.Attack(false);
}
}
// ...
}
Answer by robertbu · Mar 08, 2014 at 07:29 AM
You cannot have two functions of the same name in the same file. If you paste this in, you will get an error because the file has two Update() functions. You will need to copy the lines of code inside the Update() function in the lower script and then insert it inside the Update() function in the upper script.
For future questions, please include a paste of the error message from the console. Without it, I'm guessing a bit.
Your answer
Follow this Question
Related Questions
Unity Script Editor Not Working 1 Answer
Errors while assigning material to other object's by scripting. 1 Answer
Error line 22! Help. i dont know why? 1 Answer
(C#) Problem with animations not playing 1 Answer
Script attached to 'Object' in scene '' is missing or no valid script is attached. 5 Answers