Simple raycast Not Firing.

So I have broken my sene down into a very simple, scene with one “Cube” to test my targeting solution,
I get Debug.log “MouseDown”
however I do not get Debug.log “RayCasted” (after ray-cast has been fired, and before it hits anything.)
So apparently my Raycasts are not Firing at all.
this is for the main controll-system and function all of the Capital ships for my game.
Everything is layered correctly.

(Using Layer Masks to only Select selectableObjects that are then Assigned to “Target.Gameobject”)
then I can do stuff. (follow target, train my guns to this large, launch fighters at this target act.) :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TargetIngSystem : MonoBehaviour {


	Ray ray;
	RaycastHit hit;
	public int number = 0;
	public GameObject Target;
	public LayerMask LayerMaskSC;

	public Targeted Locked;
	public bool IsTargeted;

	//-------------------------------------------------------------------------------------

	//-------------------------------------------------------------------------------------

	//-------------------------------------------------------------------------------------

	void Update () {
		if (Input.GetMouseButtonDown (0)) {
			Debug.Log ("MouseDown");
			// Reset ray with new mouse position
			ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
			if (Physics.Raycast(transform.position, transform.forward, out hit, Mathf.Infinity, LayerMaskSC)){
				Debug.Log ("RayCasted"); // NOT FIREING
				if (hit.collider.gameObject) {
					Debug.Log ("Hit");
					Target = hit.collider.gameObject;
				}
			}
		}    
	}
	//-------------------------------------------------------------------------------------


	}

if (Physics.Raycast(transform.position, transform.forward, out hit, Mathf.Infinity, LayerMaskSC))

Returns bool True if the ray intersects with a Collider, otherwise false.


Meaning that if your Raycast doesn’t hit any colliders then it will never get to Debug.Log() line. I suggest that you try to use Debug.DrawRay to actually see what is happening in your Scene.

Make the cube bigger on all axis except towards the camera. Also, double check your variables on the script and the cube.