[C#]Script Vector2 Component Error. Simple movement error.
Im making a simple pong game just for fun. I just finished scripting the player movements for both paddles, using almost identical scripts for both. They work fine, "w" and "s" moves the first player, and the up and down keys move the second one. However, I get this error show up in the console:
"ArgumentException: GetComponent requires that the requested component 'Vector2' derives from MonoBehaviour or Component or is an interface."
Im pretty sure this a simple fix, but I just can't figure it out. It doesn't affect the gameplay though, the script works just fine.
Here is my code. Its pretty simple.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player1_Movement : MonoBehaviour {
public float PlayerSpeed = 3;
private Rigidbody2D rb;
private Vector2 Movement;
void Start()
{
rb = GetComponent<Rigidbody2D> ();
Movement = GetComponent<Vector2> ();
}
void FixedUpdate ()
{
//Simple vertical movement code...
float Vertical = Input.GetAxis ("Vertical") * PlayerSpeed;
Movement = new Vector2 (0, Vertical);
//If I let go of the said keys...
if (Input.GetAxis("Vertical") == 0)
{
rb.velocity = new Vector2(0,0); //stop the player immediately
}
rb.MovePosition (rb.position + Movement * Time.deltaTime);
}
}
Answer by UnityCoach · Mar 30, 2017 at 11:46 AM
Movement = GetComponent<Vector2> ();
Vector2 is a struct type, not a component type. It simply contains data that you can feed components with. Simply remove this line, as you don't need it, unless you want to do something I didn't get.
Your answer
![](https://koobas.hobune.stream/wayback/20220612114243im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with Score/Highscore script 0 Answers
Unity Error Script Vector2 0 Answers
How can I get the player to face the direction it is going in a Unity 2D Game? 0 Answers
I can't seem to rotate my character depending on what side he is running to (2D sidescroller) 0 Answers
Moving moving Object with left/right arrow keys in a circular direction 3 Answers