Making a simple pong clone but I cannot get the CPU paddle to follow the ball. I am having this issue because the paddle refuses to see the script/variable in the cloned ball (It will work if I set the ball to start on screen but will stop functioning once the ball respawns. Please help.
using UnityEngine;
using System.Collections;
public class BallScript : MonoBehaviour {
public AudioSource blip;
public Caller other;
public float ballx;
// Use this for initialization
void Start () {
rigidbody.AddForce(0, -150.0f, 0);
}
// Update is called once per frame
void Update () {
ballx = gameObject.transform.position.x;
if (gameObject.transform.position.y <= -1.0f) {
Destroy(gameObject);
other.ContinueSpawn();
}
if (gameObject.transform.position.y >= 20.0f) {
Destroy(gameObject);
other.ContinueSpawn();
}
}
void OnCollisionEnter(Collision Collision)
{
this.audio.Play();
rigidbody.velocity = rigidbody.velocity * 1.1f;
}
}
And the paddle script.
enter code hereusing UnityEngine;
using System.Collections;
public class CpuPaddle : MonoBehaviour {
public BallScript other;
public float ballx;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
other.GetComponent<BallScript>();
ballx = other.ballx;
if (ballx < gameObject.transform.position.x)
{
transform.Translate(-10f * Time.deltaTime, 0, 0);
}
if (ballx > gameObject.transform.position.x)
{
transform.Translate(10f * Time.deltaTime, 0, 0);
}
}
}
Thanks guys.