I’m new to Unity; currently I am taking the Walker Boys training. While using Unity 3.5 I have run in to an error I cannot figure out. I used Unity scripting references in order to formulate my code. Even thought everything I have written below compiles using Mono, unity error console is outputing a NullReferenceException error with my “Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition)”. Any assistance anyone could provide would be greatly appreciated. Below is my full script.
using UnityEngine;
using System.Collections;
public class player : MonoBehaviour {
//Player Script
//Public Varuables
public string tagName;
public float rayDistance = 0.0f;
public int playerScore = 0;
public float gameTime = 20;
public int numberOfPointsToWin = 13;
//Private Varuables
// Use this for initialization
void Start ()
{
InvokeRepeating(“CountDown”,1.0f,1.0f);
}
// Update is called once per frame
void Update ()
{
//Allows user interaction with mouse
if(Input.GetMouseButtonDown(0))
{
***Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);***
RaycastHit hit;
if(Physics.Raycast(ray,out hit, rayDistance))
{
print("Hope this works");
if(hit.transform.tag == tagName)
{
print("Hit object");
//Vector3 location = new Vector3(Random.Range(-3.3f,3.3f), Random.Range(-2.4f,2.4f),0);
//hit.transform.position = location;
GameObject clicks;
enemyScript scriptEnemy;
scriptEnemy = hit.transform.GetComponent<enemyScript>();
scriptEnemy.numberOfClicks -= 1;
if(scriptEnemy.numberOfClicks == 0)
{
playerScore += scriptEnemy.enemyPoint;
}
}
else
{
print("This object is not an Enemy!");
}
}
}
}
void CountDown()
{
if(--gameTime==0)
{
CancelInvoke("CountDown");
if(playerScore >= numberOfPointsToWin)
{
Application.LoadLevel("menuWin");
}
else
{
Application.LoadLevel("menuLose");
}
}
}
void OnGUI()
{
GUI.Label(new Rect(10,10,100,20),"Score: "+ playerScore);
GUI.Label(new Rect(10,25,100,35),"Time: "+ gameTime);
}
}