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 samaxi · Sep 18, 2013 at 10:56 AM · javascriptfpsvariable

Accessing other script problem

I have 2 scripts; Characterdamage2.js & spawn.js I want to get a variable in CharacterDamage2 and use it in spawn. Pretty much I want the variable "hitPoints" in CharacterDamage2 to +=10 with every wave in the spawn script but I don't know what I'm doing wrong.

I used this; http://docs.unity3d.com/356/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

And got this;

 var CharacterDamage2: CharacterDamage2 = hitpoints(CharacterDamage2); CharacterDamage2.hitPoints += 1.0();

Nobody helped me with this code on my old question (http://answers.unity3d.com/questions/537671/static-var-wont-work.html)

Comment
Add comment · Show 4
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 syclamoth · Sep 18, 2013 at 10:58 AM 0
Share

That is some pretty weird syntax in that code snippet right there. Looks like a mix of unityscript and c++ syntax? I'm not sure what you're actually trying to do here.

Anyway, GetComponent is your friend! You shouldn't be declaring CharacterDamage2 as a variable, since it already exists as a typename. Try something like this:

 var myDam : CharacterDamage2;

 myDam.hitPoints += 1;

Also, that's a strange name for a class. Any reason for the number 2?

avatar image samaxi · Sep 18, 2013 at 11:00 AM 0
Share

I was told to use this; http://docs.unity3d.com/356/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

avatar image samaxi · Sep 18, 2013 at 11:05 AM 0
Share

The reason for number 2, is this is kind of a $$anonymous$$i game im currently working on, so I want it separate than the real game

avatar image syclamoth · Sep 18, 2013 at 11:10 AM 0
Share

Yes, that link you have posted twice includes all the information you need to solve your problem. It also has nothing even close to that code snippet! I am really not sure what part of it is confusing you.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Pecek · Sep 18, 2013 at 12:23 PM

Details! The snippet says

 var otherScript: OtherScript = GetComponent(OtherScript); 
 otherScript.DoSomething();

And you are trying to do it this way:

 var OtherScript: OtherScript = GetComponent(OtherScript); 
 OtherScript.DoSomething();

It's similar, but not the same. The variable in the example uses the variable "otherScript", and a script named "OtherScript". Your code should work fine if you use a different name for your variable, like this:

 var characterDamage2: CharacterDamage2 = hitpoints(CharacterDamage2); 
 characterDamage2.hitPoints += 1.0;

But I would use the solution of syclamoth, just to be sure not do any typos later.

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 flaviusxvii · Sep 18, 2013 at 12:35 PM 0
Share

The world really needs to settle on using camelCase for variables and PascalCase for classes and such. I know the .NET world is in love with PascalCase for everything, but my brain just can't get over it.. and most online syntax hilighters assume PascalCase is a classname too.

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

18 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Static var wont work 2 Answers

What is the problem with this code? 1 Answer

How to access variable from another function? 2 Answers

Why are my variables not showing up? 2 Answers


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