[SOLVED] Accessing hasStarted from another script

Hey. So I’ve decided to use Audiosource.PlayClipAtPoint for all sounds because it seems I’ll have more control over when sounds play that way. I want to use a sound for when the ball hits the paddle only, which I’ve written in the Paddle script. The only problem is it plays when the game starts! I figure I need to use “if (hasStarted)” like shown in the lecture, but of course hasStarted in the Ball script! I’ve looked around online and found that in order to access the Ball script I need to type “ball = gameObject.GetComponent()”, but it’s saying “the name “hasStarted” does not exist in the current context”. I’ve tried typing ball.hasStarted, and making a “private bool hasStarted”, but they don’t work either. Anyway, here’s my Paddle script:

https://gist.github.com/anonymous/c340889fde10d7baac19801138fd14ba

Appreciate any help, this has been driving me mad! :slight_smile:

You need to find the Object not the Component, this has worked for me:

void Start () {
	ball = GameObject.FindObjectOfType<Ball>(); }

Then you will need to use:

if (ball.hasStarted);

Hope this helps

Update: After looking it up apparently the way I suggested isn’t the optimal way and is slow, hopefully someone else will have a better answer for you.

Now that is odd. May I suggest first go to the inspector and verify that play on awake is unchecked.

1 Like

Thanks for the reply. Unfortunately it didn’t even work, let alone be slow. Having said that I do get one less error in the console when I use FindObjectOfType. Didn’t notice this before, but the error I’m getting in the console is: “‘Ball.hasStarted’ is inaccessible due to its protection level”. Hold on a minute…

It’s working! I changed the hasStarted variable in the Ball script to public and it works! Thanks my man. Still, if anyone can suggest a way that’s quicker, that would be great to. Tried to follow the first answer in that link, didn’t really get it hehe. Still, I’m happy that it’s working for the moment.

1 Like

This is still odd. I liked your idea so much that I added a sound to paddle myself!, modeling after the brick sound, and it works fine. I wonder if its your placement of the OnCollision… statement function. i placed mine after the closing curlies of update and before Autoplay(). Perhaps you are testing if the paddle is coliding, and it is, too soon.

using UnityEngine;
using System.Collections;

public class Paddle : MonoBehaviour {

public AudioClip punch;
public bool autoPlay=false;
public float minX, maxX;
private Ball ball;

void Start () {
	ball = GameObject.FindObjectOfType<Ball>();
	print (ball);
}
	
void Update () {
	if (!autoPlay){
		MoveWithMouse();
	} else {
		AutoPlay();
		}
}
void OnCollisionEnter2D (Collision2D col){;
	AudioSource.PlayClipAtPoint (punch, transform.position, 0.6f);
1 Like

I don’t really understand what you’re saying. It’s all working fine for me now. Are you saying you’re not having the problem of getting the ball hitting the paddle sound as soon as the level starts? In any case I’m glad I… inspired you. Haha. :grin: