Page pickup

Hello everyone, I’m trying to make a Slender game (just for personal programming exercise) but I’m having four compiler errors with my PageManager script. The errors are all on the same line of code: `

if (Physics.Raycast (transform.position, transform.forward, theRay, rayLength))`

Could someone tell me what I’m doing wrong and point me in the right direction?
Thank you all in advance. :slight_smile:

Here’s the code:

using UnityEngine;
using System.Collections;

public class PageManager : MonoBehaviour {
	
	public GameObject page1, page2, page3, page4, page5, page6, page7, page8;
	private int pagesCollected;
	private int pagesRemaining;
	
	void Start () {
	
		pagesCollected = 0;						// Setting the number of pages 
		pagesRemaining = 8;
	}
	
	void Update () {
		
		CollectPage ();
	}
	
	void CollectPage () {

		RaycastHit theRay;										
		float rayLength;
		if (Physics.Raycast (transform.position, transform.forward, theRay, rayLength))										// Raycasting
			if (theRay.collider.gameObject.name == "page1")
		{
			Destroy (page1);																																// Destroying the pages
			pagesCollected ++;
			pagesRemaining --;
		}
		else if (theRay.collider.gameObject.name == "page2")
		{
			Destroy (page2);
			pagesCollected ++;
			pagesRemaining --;
		}
		else if (theRay.collider.gameObject.name == "page3")
		{
			Destroy (page3);
			pagesCollected ++;
			pagesRemaining --;
		}
		else if (theRay.collider.gameObject.name == "page4")
		{
			Destroy (page4);
			pagesCollected ++;
			pagesRemaining --;
		}
		else if (theRay.collider.gameObject.name == "page5")
		{
			Destroy (page5);
			pagesCollected ++;
			pagesRemaining --;
		}
		else if (theRay.collider.gameObject.name == "page6")
		{
			Destroy (page6);
			pagesCollected ++;
			pagesRemaining --;
		}
		else if (theRay.collider.gameObject.name == "page7")
		{
			Destroy (page7);
			pagesCollected ++;
			pagesRemaining --;
		}
		else if (theRay.collider.gameObject.name == "page8")
		{
			Destroy (page8);
			pagesCollected ++;
			pagesRemaining --;
		}
	}
}

1 Answer

1

Two problems first you in C# you need to use the keyword ‘out’ with the ‘RaycastHit’ parameter. In addition, you did not assign a length to your ‘rayLength’ variable. Try this:

float rayLength = 10.0f;
if (Physics.Raycast (transform.position, transform.forward, out theRay, rayLength))

Thank you very much, it works now. :)

Please don't post comments as answers. I moved your comment to the appropriate place.