Newbie question

Helo guys I would like to know what is wrong with this script. When I’m accessing the IEnum on the other script 1 it’s not working though it didn’t have an error.

Script 1
Rigidbody rbBall;
public Vector3 ballPosition;
float ballSpeed = 50f;

private void Start()
{
rbBall = GetComponent();
transform.position = ballPosition;
StartCoroutine(BallStart());
}

public IEnumerator BallStart()
{

 yield return new WaitForSeconds(5);
 rbBall.constraints = RigidbodyConstraints.FreezePositionY;
 float xBallPath = 1/*Random.Range(-2, 2)*/;
rbBall.velocity = new Vector3(xBallPath * ballSpeed, 0, 0 );

}

Script 2
public int scorePlayer2;
GameObject ball;
Ball ballScript;

private void Start()
{
ball = GameObject.Find(“Ball”);
ballScript = ball.GetComponent();

}

private void OnCollisionEnter(Collision collision)
{

  if(collision.gameObject.name == "Ball")
  {

      Debug.Log("Hit");
      scorePlayer2++;
      collision.rigidbody.velocity = Vector3.zero;
      StartCoroutine(ballScript.BallStart());
  }

}

Hi Flipped_Ego,

Welcome to our community! :slight_smile:

In which course and lecture are you? What does ‘it’s not working’ mean? What did you expect to happen? What happened instead?


See also:

Initially, the ball is pushed in one direction. Presumably it hits an object with script 2 on it. Script 2 stops it and starts the coroutine which will wait 5 seconds and then just try to send the ball in the same direction, causing nothing to happen because it’s stuck against the object it initially hit. Now nothing further happens because it isn’t hitting that object again because it is still against it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms