- Home /
The question is answered, right answer was accepted
Slenderman like game pages not working!
What I want is to be able to pick up pages like in Slender. Here is my script: (By the way this is a modified version of Ryandomes script)
var Paper : int = 0;
var paperToWin : int = 30;
var AllowPickup : boolean;
function OnTriggerEnter (other.Collider)
{
if (other.gameObject.tag == "Paper")
{
AllowPickup == true;
}
}
function OnTriggerExit (other.Collider)
{
if (other.gameObject.tag == "Paper")
{
AllowPickup == false;
}
function(Update)
{
if(AllowPickup == true)
{
if(GetKeyDown("e"))
{
if (other.gameObject.tag == "Paper")
{
Paper += 1;
Debug.Log("A paper was picked up. Total papers = " + Paper);
Destroy(other.gameObject);
}
}
}
}
function(OnGUI)
{
if (Paper < paperToWin)
{
GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "" + Paper + " Papers");
}
else
{
GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "All Papers Collected!");
}
}
I get These error messages:
Assets/ObjectCounter.js(5,31): BCE0043: Unexpected token: ..
Assets/ObjectCounter.js(13,30): BCE0043: Unexpected token: ..
Assets/ObjectCounter.js(34,10): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/ObjectCounter.js(46,10): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/ObjectCounter.js(47,1): BCE0044: expecting }, found ''.
Please help!
Answer by Caiovvs · Nov 26, 2014 at 02:12 PM
You should be doing this
function OnTriggerEnter (other : Collider)
Because you have to treat "other" like a variable of type Collider. Also you forgot the closing brackets in the 'OnTriggerExit' function.
There are other incorrect things so I tried fixing your script, just see if this works for you:
var Paper : int = 0;
var paperToWin : int = 30;
var AllowPickup : boolean;
var paperObject : GameObject;
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.tag == "Paper")
{
paperObject = other.gameObject;
AllowPickup = true;
}
}
function OnTriggerExit (other : Collider)
{
if (other.gameObject.tag == "Paper")
{
AllowPickup = false;
}
}
function Update()
{
if(AllowPickup == true)
{
if(Input.GetKeyDown(KeyCode.E))
{
if (paperObject.tag == "Paper")
{
Paper += 1;
Debug.Log("A paper was picked up. Total papers = " + Paper);
Destroy(paperObject);
}
}
}
}
function OnGUI()
{
if (Paper < paperToWin)
{
GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "" + Paper + " Papers");
}
else
{
GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "All Papers Collected!");
}
}
Thanks so much! Its almost working now, but as soon as I pick up a paper the GUI disappears! Please help!
I think you should be handling the number of papers left and other GUI in another script, maybe attached to the player or a manager game object. What is happening is that when you pick up the paper, it is destroyed, and the script(which also handles the GUI) stops working. Edit: Scratch that, just realized you attached it to the player probably.
So any ideas? Just making sure you know I am a new to coding.
I tested it and it worked fine. I added a cube with a trigger box collider and the 'Paper' tag, and a fps controller with your script. Then I walked over to the cube, pressed 'E' and it disappeared, but it still showed the number of papers at the top. What GUI are you refering to?
Ahhh I fixed it, I by mistake checked "Allow Pickup" Sorry about this. Thanks for everything!
Answer by Wisearn · Nov 26, 2014 at 02:08 PM
You need a closing bracket for
function OnTriggerExit (other.Collider)