- Home /
Trouble getting variables from cloned scripts.
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.
what is "other" in this second script? Looks undefined
Other in the second script is defined through drag/drop in the editor.
Answer by Lockstep · Jan 25, 2014 at 02:58 AM
The destroyed ball and the instantiated ball are two different gameObjects. Therefore the cpu paddle can't possibly carry over the reference to it.
You can either have it look for a new ball when the old was destroyed (rough code, requires some more setup):
if(other == null)
other = GameObject.FindWithTag("Ball");
Or instead of destroying the old ball just reuse it:
void RespawnMe(){
transform.position = startposition;
rigidbody.velocity = Vector3.zero;
rigidbody.AddForce(0, -150.0f, 0);
}
Answer by Vendettaseve · Jan 25, 2014 at 04:34 AM
At the end of the day, this is how it all got fixed. Thanks Lockstep for putting me in the right direction.
using UnityEngine;
using System.Collections;
public class CpuPaddle : MonoBehaviour {
public GameObject other;
public float ballx;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (other == null) {
other = GameObject.FindWithTag("ball");
Debug.Log("ball aquired");
}
ballx = other.GetComponent<BallScript>().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);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613131352im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
System.Collections error while building the player for windows phone 8 1 Answer
Crashing of unity 0 Answers
Need help to earn Krama 2 Answers
Help! My script wont work! 1 Answer