Help me please i am stuck.
So this is the code , and the message i get is The call is ambiguous between the following methods or properties: UnityEngine.Vector2.operator +(UnityEngine.Vector2, UnityEngine.Vector2)' and
UnityEngine.Vector3.operator +(UnityEngine.Vector3, UnityEngine.Vector3)'
using UnityEngine; using System.Collections;
public class ball : MonoBehaviour {
private Paddle paddle;
private Vector3 paddleToBallVector;
private bool hasStared = false;
// Use this for initialization
void Start () {
paddle = GameObject.FindObjectOfType<Paddle>();
paddleToBallVector = this.transform.position - paddle.transform.position;
print (paddleToBallVector);
}
// Update is called once per frame
void Update () {
if (!hasStared) {
this.transform.position = paddle.transform.position + paddleToBallVector;
}
if (Input.GetMouseButtonDown(0)) {
print ("Left mouse Clicked, Launch ball");
hasStared = true;
this.GetComponent<Rigidbody2D>().velocity = new Vector2 (3f, 7.5f);
}
}
void OnCollisionEnter2D (Collision2D collision){
//Ball does not trigger sound when brick is destroyed
//Not 100% sure why, possibliy because there isnt a brick
Vector2 tweak = new Vector2 (Random.Range(0f,0.2f), Random.Range(0f,0.2f));
if ( hasStared) {
GetComponent<AudioSource>().Play();
GetComponent<Rigidbody>().velocity += tweak;
}
}
}
console error: The call is ambiguous between the following methods or properties: UnityEngine.Vector2.operator +(UnityEngine.Vector2, UnityEngine.Vector2)' and
UnityEngine.Vector3.operator +(UnityEngine.Vector3, UnityEngine.Vector3)'
Answer by TBruce · Aug 28, 2016 at 05:41 PM
In OnCollisionEnter2D() change this
GetComponent<Rigidbody>().velocity += tweak;
to this
GetComponent<Rigidbody2D>().velocity += tweak;
but instead may want to make the following changes to your class like so
using UnityEngine;
using System.Collections;
public class ball : MonoBehaviour
{
private Paddle paddle;
private Vector3 paddleToBallVector;
private bool hasStared = false;
private AudioSource audioSource;
private Rigidbody2D rigidbody2D;
// Use this for initialization
void Start ()
{
paddle = GameObject.FindObjectOfType<Paddle>();
paddleToBallVector = this.transform.position - paddle.transform.position;
print (paddleToBallVector);
audioSource = GetComponent<AudioSource>();
rigidbody2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update ()
{
if (!hasStared)
{
this.transform.position = paddle.transform.position + paddleToBallVector;
}
if (Input.GetMouseButtonDown(0))
{
print ("Left mouse Clicked, Launch ball");
hasStared = true;
rigidbody2D.velocity = new Vector2 (3f, 7.5f);
}
}
void OnCollisionEnter2D (Collision2D collision)
{
//Ball does not trigger sound when brick is destroyed
//Not 100% sure why, possibliy because there isnt a brick
Vector2 tweak = new Vector2 (Random.Range(0f,0.2f), Random.Range(0f,0.2f));
if ( hasStared)
{
audioSource.Play();
rigidbody2D.velocity += tweak;
}
}
}
Omg, i feel really dumb, :( spent over 30 $$anonymous$$tues to try to fix the error and at the end i found it and yep that works, Thank you soo much