I’m Having some trouble with a code I need to make the player die when in a box collider of an enemy, but I keep getting
error CS1001: Identifier expected
when I try to use it in the game. if someone can help me fix this issue, it would be well appreciated!
Here is My Code, if you would like to check it for errors:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EntityKillerScript : MonoBehaviour
{
function OnTriggerEnter(other : Collider)
{ //this function checks for the collision of the local Collider which is Trigger (i.e. field-like, and not rigid, objects can pass through the field, but will trigger the function)
if (other.gameObject.name == "Player")
{ //the function took an argument "other", it refers to a collider that is passing through the trigger field
GameObject.Destory(other.gameObject); //using our keyword that refers to that external collider, it picks the whole gameObject that somehow relates to that collider, that is, holds it. We than destroy that gameObject
for (var child : Transform in transform.gameObject),
{ //
GameObject.Destroy(child.gameObject); //those three lines are only needed if you have children of the player objet that have to be removed
} //otherwise, you can remove those 3 lines
Debug.Log("YOU DIED"); //write a message in a console that the player has r.i.p'ed
}
}
}
You seem to have copied some code what from a old language that Unity doesn’t use any more.
Best to learn from resources that use C#, which is the only language Unity uses since forever ago.
That code is MUCH more readable if you omit the comments:
public class EntityKillerScript : MonoBehaviour
{
function OnTriggerEnter(other : Collider)
{
if (other.gameObject.name == "Player")
{
GameObject.Destory(other.gameObject);
for (var child : Transform in transform.gameObject),
{
GameObject.Destroy(child.gameObject);
}
Debug.Log("YOU DIED");
}
}
}
Comments should be used sparingly, not to explain code, nor to repeat what the code already expresses.
This then makes it easier to spot such syntax errors:
for (var child : Transform in transform.gameObject),
There’s a trailing comma that mustn’t be there.
Also in the next line you have a typo. The IDE will highlight this for you pretty noticeably.
Note that these error messages have the format:
.../SomeScript.cs(123:45)
Where 123 is the line number and 45 the column/character in that line. It doesn’t always point to the error precisely, sometimes it’s just the next statement that stopped making sense.
In any case double-clicking the error in the Unity Console window should highlight the line the error occured on.
Oh right, now that you mention it … function .. other:Collider
You’re mixing up random languages, looks like maybe UnityScript and C# mooshed together… that will never work.
Slow down and back up and review what you have done before this, which is C#, so you can understand where you need to go next. You can’t just keep throwing code in without understanding it.
Two steps to tutorials and / or example code:
- do them perfectly, to the letter (zero typos, including punctuation and capitalization)
- stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.
Imphenzia: How Did I Learn To Make Games: