- Home /
The question is answered, right answer was accepted
Simple problem regarding script copying
I'm trying to apply a script to a 2DRigidbody sprite. I simply cant figure this out. I found a movement script on Github that I would like to apply to my sprite, but I cannot find how to add the component. Thanks. I have Visual Studio.
Hey, Can you share the movement script you found please? There are number of different ways a script can access the components of a game object.
Here it is, it derives from this tutorial btw: https://www.youtube.com/watch?v=TcranVQUQ5U
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float speed;
private Rigidbody2D body;
private void Awake()
{
body = GetComponent<Rigidbody2D>();
}
private void Update()
{
body.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, body.velocity.y);
if (Input.GetKey(KeyCode.Space))
body.velocity = new Vector2(body.velocity.x, speed);
}
}
After watching the tutorial I understand the copy-paste maneuver, but it simply doesn't work on my end. the error code that unity provides: "No MonoBehaviour scripts in the file, or their names do not match the file name." Could it be that I'm missing the SDK on my Visual Studio?
Answer by Zeliktric · Jan 12 at 04:49 PM
Add the script to your Assets/ folder. Then after Unity has added it, you can simply drag it to the gameobject that you want the script on :)
Answer by Tracker909 · Jan 13 at 07:52 AM
sorry for this lazy post, after a bit of tinkering with compiler errors I got this to work. Thanks to all who responded! :)
Follow this Question
Related Questions
How to implement tokenization of game characters? 1 Answer
Can't display my Countdown timer text on my end menu. (newbie needing help) 1 Answer
How to restrict first person from turning 360 degrees when sitting? 1 Answer
Beginner Question: How to get normals from a physics raycast using visual scripting? 0 Answers