Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nercoe · Sep 25, 2012 at 07:34 PM · collisionvariablereferencescore

Javascript score problems whilst referencing scripts.

Hey guys, I've recently been told I need to learn JavaScript as I'm in my second year of university studying Games Design and am having a little trouble. Here's my problem:

When I hit an enemy (chicken) with my bullet all is well and good and the enemy disappears and does what it is supposed to. I then tried to add a scoring system that gives you +1 to an assigned variable as you hit the 3 targets so the final score would be 3 (add 1 each time). This was fine but when I added the score the gameobject deleted with my destroy function and with that went the assigned variable.

Moving on, to counter that problem I called upon the other script from my gui script which I thought would fix the problem. However, the score doesn't seem to be adding up (it is not deleting this time) and I cannot see the problem, and yes I have dragged the object from the hierarchy into the script variable slot. Here is my code:

This is my GUI code:

 var style : GUIStyle;
 var collisionchicken : CollisionChicken;
 var chickenHit : int = 0;
 
 function OnTriggerEnter(c:Collider){
         if(c.gameObject.tag =="bulletShot")        
     {
         chickenHit++;
           
         }
     }
     
 function OnGUI()
 {

     GUI.Label( Rect(515, 10, 100, 20), ""+ chickenHit.ToString("f0"), style );
 
 }  


And here is my script that destroys the chickens:

 var explosion : Transform;
 var Bwuk : AudioClip;
 
 function OnTriggerEnter(c:Collider){
         if(c.gameObject.tag =="bulletShot")
     {
         audio.PlayOneShot(Bwuk);
             yield WaitForSeconds(0.3);
              Destroy(gameObject);
      var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
         }
     }


In case I have missed anything, I will recap quickly. I want the chickenHit variable to add 1 each time the object is destroyed in the external script but at the moment it stays at 0 :( I know you guys are extremely knowledgeable and look forward to learning from you :)

Thanks, Jimmy

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nercoe · Sep 25, 2012 at 09:59 PM 0
Share

Sorry, my apologies. The name of the script that contains the variable chickenHit is: $$anonymous$$enu.js The name of the collision script is: CollisionChicken.js

The gameObject attached is called chicken1

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by RobbynB · Sep 26, 2012 at 12:21 AM

I see a couple of things... since I too have recently learned JavaScript and have had issues with score.

code should be

//this gives you the ablity to select a GUIText object from the inspector
var Scorecount : GUIText;
//this is so the count can be read as a string
var scorePrint;
//This allows you to select what the trigger is
var collisionchicken : GameObject;
var chickenHit : int = 0;
//These are for when the object is destroyed
var explosion : Transform;
var Bwuk : AudioClip;

function OnTriggerEnter(c:Collider)
{
if(c.gameObject.tag =="bulletShot")
{
chickenHit+1;
//This will destroy the gameobject that has the trigger on it.
Destroy (collisionchicken);
}
}
function Update()
{
scorePrint = chickenHit.ToString();
Scorecount.text = String.Format(scorePrint);
}

I know I forgot to add your sound and explosion but you should be able to figure out where those go...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nercoe · Sep 26, 2012 at 12:50 AM 0
Share

A huge help to me! Putting me on the right lines for sure, thank you very much! Just be careful with chickenHit+1, make sure it's either chickenHit = chickenHit +1 or chickenHit++. Probably just a typo but just in case! Thank you very much for the help, I appreciate it :)

Additionally I tried to thumbs up you but apparently I cannot do it on this account, I am unsure if I can.

avatar image
0

Answer by DaveA · Sep 25, 2012 at 11:07 PM

I would combine these scripts unless you have a good reason to keep them apart. OnTriggerEnter is doing the same amount of work, so just combine those.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nercoe · Sep 25, 2012 at 11:24 PM 0
Share

I have taken what you said on board and thanks for the reply. Part of the problem is though, it deletes the script when the object is destroyed thus removing the score variable which therefore leads to no output.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

access a var from another script 3 Answers

Howto access global var from other script? 2 Answers

Collision object and access to a script variable? 1 Answer

Using a string in a C# script in a .js 1 Answer

Unity3d Javascript Reference Script Variable 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges