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 smirlianos · Aug 23, 2012 at 04:44 PM · javascriptobjectvariablesaccessvar

Help with variables

Hello!

I have this script

function OnTriggerEnter (other : Collider) { if(used == false) { // Find the child named "ScoreHolder". // On the OtherScript attached to it, set score ++. transform.Find("ScoreHolder").GetComponent(Counter).score ++; } }

function OnTriggerExit (other : Collider) { if(used == false) { used = true; } Destroy(destroyedobj); }

that every time the "player" walk into a "box", the other script

var score : int = 0;

function Update () {

if(score == 5) { //something happens score = 0; } }

change the score,but the object with the second script must be a child to the first one. The problem is that I can't have the second script a child to every "box" that the "player" walks into...

What can I do? Please help...

Comment
Add comment
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

2 Replies

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

Answer by brunopava · Aug 23, 2012 at 08:20 PM

Make a script like this:

 function OnTriggerEnter (other : Collider)
 { 
     if(other.gameObject.tag == "Player")
     {
         other.gameObject.SendMessage ("ApplyScore", incomingScoreValue);
         GameObject.Destroy(gameObject);
     }
 }

And attach is to the boxes.

Then on any of the player's scripts you put a function like this:

 function ApplyScore(incomingScore:int)
 {
     //do whatever you want with the score
 }
Comment
Add comment · Show 4 · 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 smirlianos · Aug 24, 2012 at 10:00 AM 0
Share

it has an error "$$anonymous$$ identifier: 'inco$$anonymous$$gScoreValue'."

avatar image brunopava · Aug 24, 2012 at 01:20 PM 0
Share

you are suposed to declare a variable with that name, or put a float value there.

avatar image smirlianos · Aug 25, 2012 at 05:21 PM 0
Share

becouse I'm noob, a var in the first script like that? var inco$$anonymous$$gScoreValue : float = 0;

avatar image Hybris · Aug 25, 2012 at 06:36 PM 0
Share

exactly! you got it right with the variable, of course remember that you can call your variables anything you dont have to do what others do! :D

avatar image
0

Answer by Kryptos · Aug 23, 2012 at 05:23 PM

Put the second script in a child of the player object and the first one on the player.

Comment
Add comment · 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

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

10 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

Related Questions

Access other variables please help :( 1 Answer

Is it possible to create new variables by incrementing? 1 Answer

Trying to access a variable in a different script on a different game object. 1 Answer

Accessing a js static var from a c# script 1 Answer

One of my scripts can't find the variable from this script 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