If raycast hits me?

Hi,

I’ve just started to use Unity, so evidently, I suck at this stuff. So, please bear with me.

Here is my story:

I’ve got a transparent wall in front of my camera. Behind the wall, I’ve got about 20 mini spheres. I wanted unity to Debug a word when a sphere is clicked. So I used raycast and masked out the wall by putting the wall on a different layer. I’ve got 2 scripts; a script for the camera; and a script for the spheres. I attached the sphere script on all of the spheres and did the same with the camera script. The Camera has the raycast script and the spheres have the debug script.

I created a variable on the camera’s script and called it true; when it hit a sphere. On the sphere’s script, I waited for the variable to turn true and call the debug. However, it does it for all of the spheres regardless of what it hit.

What I’m trying to do is somehow add a if (I got hit by the raycast on the script that is attached to the camera && the variable is true) {

debug

}

How would I go about doing that? Would that even be possible on Unity?

Here is the script placed on the Camera:

static var Spawn = false;

static var Hit : RaycastHit;
var Range : float = 50;
var layerMask = 1 << 8;
layerMask = ~layerMask;

function Update () {
        var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        
        Debug.DrawRay(ray.origin, ray.direction * Range, Color.green);
        
        
        if (Physics.Raycast (ray, Hit, Range, layerMask) && Input.GetMouseButton(0) && Spawn == false) {
        
        	Spawn = true;
        
        	}

And here is the script attached to the spheres:

var prefab : Transform;
private var on = true;



function Update() {

if (Raycast.Spawn == true && on == true) {

	Debug.Log("HEY");
	on = false;
	}

}

So, again, how would I be able to tell the script attached to the sphere to only debug when it was hit by the raycasy on the script that is attached to the camera?

Any help would be much appreciated.

Make your ‘on’ public. Then inside your raycast you do something like:

var go : GameObject = hit.collider.gameObject;
if (go != null) {
    var otherScript: BallScript = GetComponent(BallScript);
    if (otherScript != null)
        otherScript.on = true; 
}

That way the raycast only sets the ‘on’ for the single ball it hits. Here is a link to a page concerning accessing variables across game objects.

Accessing Other Game Objects

static var Spawn = false;

static var Hit : RaycastHit;
var Range : float = 50;
var layerMask = 1 << 8;
layerMask = ~layerMask;
var go : GameObject = hit.collider.gameObject;
function Update () {
        var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        
        Debug.DrawRay(ray.origin, ray.direction * Range, Color.green);
        
        
        if (Physics.Raycast (ray, Hit, Range, layerMask) && Input.GetMouseButton(0) && Spawn == false && go != null) {
        	Debug.Log("Ray hit: "+Hit.collider.gameObject.name);	
        	//Instantiate(prefab, Hit.point, transform.rotation);
        	Spawn = true;
        	

            
        
        
        }
        
        		if (go != null) {
		var otherScript: Spawn = GetComponent(Spawn);
		if (otherScript != null)
		otherScript.on = true;
 }
        
        }

Hi,

Sorry for the late reply, this is the script I attached the Camera. This is how I changed it. “Spawn” is what the script attached to the Spheres is called.

This is the error I’m getting:

Assets/Scripts/Raycast.js(21,23): BCE0005: Unknown identifier: 'hit'.

What do I do next? I have no clue what the link is talking about.

Thanks for all the help.