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 xSpectrum · Jul 22, 2012 at 09:32 PM · functionclass

Accessing function from another class?

I have one script with the following:

 class Weapon
 {
       function SpawnWeapon()
      {
           print("It Works!");
      }
 }

And another that tries to access the function:

 var theWeaponScript : Weapon;
 function Update ()
 {
     theWeaponScript.SpawnWeapon();
 }

It doesn't give me an error, but it doesn't print out anything, either.

Also, I don't think this is related, but I have another class in the same script as the "Weapon" class. Unity gives me a warning that "The class defined in script file does not match the file name".

Comment
Add comment · Show 2
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 Linus · Jul 22, 2012 at 09:41 PM 0
Share

The file name is Weapon.js? Try to have the custom class another name than the file name. All js scripts are actually a class, so it may be some name conflict occurring.

avatar image xSpectrum · Jul 22, 2012 at 10:39 PM 0
Share

File name is WeaponAttributes.js

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Jul 22, 2012 at 09:46 PM

You declare theWeaponScript variable to be of type Weapon which is a class. You need to make it of the type script name.

Also, because the function is a member method (belongs to a class) it is called through an instance of an object of type Weapon.

Edit: I actually wonder what is the name of your script, would it be Weapon.js? In this case, that would be the reason you do not get any error. You have a conflict name between the class and the script. Best would be to change one and then access the script like:

 var script:ScriptName;
 
 function Start(){
     script =GameObject.Find("ObjectName").GetComponent(ScriptName);}
 
 function Update(){
     script.instanceName.functionName();
 }


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 xSpectrum · Jul 22, 2012 at 10:39 PM 0
Share

While a valid solution in theory, it doesn't exactly fit what I want because there will be many weapons of the same name in the game, not having distinct names... I was following the theory from a Youtube tutorial and he didn't do what you did: https://www.youtube.com/watch?v=p8fiHeh$$anonymous$$ubs

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

6 People are following this question.

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

Related Questions

Instantiate inside class function returns NullReference 1 Answer

The name `PurchaseManager' does not exist in the current context 0 Answers

Make inspector show changes of class variable value in function? 1 Answer

Not sure why my function isn't working right. 0 Answers

Extended class functions 0 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