Please help- Javascript not working at all and I have no idea why

Alright, so I should start off by saying I’m not a coder, and I am very new to the idea of coding my own stuff completely. I’ve just created my own script which seems good to me, but for some reason has 2
1). I get an error telling me that it ‘does not denote a valid file type’, which refers to ‘if (collision…’
2). The public vars no longer show up in inspector.

Alright so heres the code

the idea of this would be to create a scoring system where if the head of my player touches the back of another, it adds 100 score to the gui text. The way this should be done in the code is I define what EnemyTriggerOne is as the back of one of the 3 enemies, in the inspector. it then detects when there is a collision between these two (since I attach this script to the head of my player) it adds 100 score.

What have I done wrong?

im no expert in unityscript, but i think that “collision: EnemyTriggerOne” should be with capital C, “Collision:blabla”

EDIT: and sorry i didn’t really read your code before, when you do “Collision : something”, that should be another name ( instead of EnemyTriggerOne), use something like “objectCollided”, and then

Tried that, got near enough to the exact same error, remember it is javascript though.

I didnt try the capital C thing because apparently thats not the problem, the objectCollided is.

any help?

What you are doing is not the way to do it.

function OnCollisionEnter(collision : Collision){
if (collision.gameObject.name == EnemyTriggerOne.name){
score += 100;
}
guiText = score + "pts";
}

Try that?

So close, but this looks like an easy fix. Any help?

‘guiText’ is a class (in this case a component), you have to access its text property.

guiText.text = "whatever you want";

I want it to increase by 100 every time this happens, as it says with ‘score += 100’ above. So how would I display the score?

Oh wait I get it I just need .text.

Thank you so much both of you, this was really getting on my nerves but Im delighted to finally get it done!

Just the way you’ve done it. I just wrote the nonsense text there because i was lazy to scroll up again. Haha :stuck_out_tongue:

guiText.text = score + "pts";

and you could also place it into the if statement, because it doesn’t need to be refreshed when nothing changes it anyways.

Thanks mate, this was massively helpful. Best thing, I learnt something from it!

Hey, sorry about this, it is working, but how would I make this show up on GUI text. I’ve got an error saying

(Player NewFlare is the name of my player btw)

But thats not really the problem. The problem is when I child it to the player or the main camera…
1). It moves to the left or the right when I move forwards or backwards
2). It doesnt display the score

Well, you can attach a guiText to your gameObject, that would fix the error.

However, if you then move your gameObject around, you’ll notice it moves in a strange way. Either attach it to a static, non-moving object, position it and set a reference to that script in your other script, or just use simply MonoBehaviours OnGUI() function in order to draw the text on your screen.

I dont really understand what you mean, basically I attach the GUI text to the player gameobject, and when the player gameobject moves the text moves to the side. You lost me from there

// remove guiText.text = "whatever you want" from on collision enter and use this function instead
function OnGUI()
{
     GUI.Label(Rect(10,10,100,200), score + "pts");
}

I hope that’s the correct JS syntax, i’m usually coding in C# :S

Test a bit for your desired offset and watch here for more information

You are fantastic mate! Now I just need to find out how to change font and size, but you’ve done enough, if you ever need any help with any models as a thanks I’d be happy to help!

You could either pass a modified guiSkin as a parameter or change the attributes of the current GUISkin directly via Gui.skin.label.xyz (xyz = the attribute you want to alter).

Or you choose you put a guiText in your scene which cannot be moved and access it via script… that is also a good way if you prefer working in the inspector. Gotta go to bed now though, i’ll come back tomorrow.

Alright, Im back with unfortunately another issue. So, first of all I should say that I changed the public vars to gameobjects because it works better for me. Anyway, the idea of this was to work alongside another script which means that if anything collides with the back of the player, not only does it add score, but it also kills the player who was hit. Apart, these two scripts worked, but together, they didnt. The character would be destroyed but the score wouldnt increase.
This is the collision script, its in C# btw

So I assumed that it had to do with timing, so I added a yield return new to the script, so it became this

However, now I get an error, stating

Which has stumped me. Any ideas?

If you want to yield something, ist not void OnTriggerEnter, but IEnumerator OnTriggerEnter. void means “there is no return value”

Hmm, well that worked, but its not increasing the score still, which is from the other script (bear in mind that the other script isnt mentioned in this script so I dont believe moving it to standard assets is a fix)

Maybe its something to do with which script executes first, could that be it? If so, how would I change that, and if not any other solutions would be helpful

What’s your current setup?

You got that script with OnCollisionEnter added to your player character and that character has got a child object which is the head. And this head got the other script with OnTriggerEnter and is marked as isTrigger?