Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by ostrich160, May 5, 2014.

  1. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    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?
     
  2. geroppo

    geroppo

    Joined:
    Dec 24, 2012
    Posts:
    140
    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
     
    Last edited: May 5, 2014
  3. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    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?
     
  4. TheMeanCoder

    TheMeanCoder

    Joined:
    Apr 25, 2014
    Posts:
    15
    What you are doing is not the way to do it.

    Code (csharp):
    1.  
    2. function OnCollisionEnter(collision : Collision){
    3. if (collision.gameObject.name == EnemyTriggerOne.name){
    4. score += 100;
    5. }
    6. guiText = score + "pts";
    7. }
    Try that?
     
  5. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    So close, but this looks like an easy fix. Any help?

     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    'guiText' is a class (in this case a component), you have to access its text property.

    Code (csharp):
    1.  
    2. guiText.text = "whatever you want";
    3.  
     
  7. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    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!
     
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Just the way you've done it. I just wrote the nonsense text there because i was lazy to scroll up again. Haha :p

    Code (csharp):
    1.  
    2. guiText.text = score + "pts";
    3.  
    and you could also place it into the if statement, because it doesn't need to be refreshed when nothing changes it anyways.
     
  9. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Thanks mate, this was massively helpful. Best thing, I learnt something from it!
     
  10. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    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
     
  11. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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.
     
  12. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    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
     
  13. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Code (csharp):
    1.  
    2.  
    3. // remove guiText.text = "whatever you want" from on collision enter and use this function instead
    4. function OnGUI()
    5. {
    6.      GUI.Label(Rect(10,10,100,200), score + "pts");
    7. }
    8.  
    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
     
  14. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    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!
     
  15. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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.
     
  16. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    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?
     
  17. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    If you want to yield something, ist not void OnTriggerEnter, but IEnumerator OnTriggerEnter. void means "there is no return value"
     
  18. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    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)
     
  19. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    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
     
  20. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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?
     
  21. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    I've got the score script on the head of the player and the collision destroy script that I just posted on the back cube of a 2 cube NPC kind of (but it only has those scripts attached, and the collision boxes + a rigibody). the back cube of the NPC has the collider set to trigger, or course. SO when the back cube of the NPC is hit, it is destroyed, and because the NPC back is linked to the player as EnemyTrigger1, it should add the score, but it doesnt
     
  22. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    I've found the problem, its to do with my collision script. It requires me to set the box collider to is trigger, which breaks the scoring system. It would probably be easier to just make a collider script which doesnt require the collider to be set to is trigger. I dont mind if its in javascript, but I need help with it. One important thing about it though, it cant use rigibodies.
     
  23. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679