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 slidecoast · May 23, 2013 at 12:11 AM · gameobjectcolliderscoreshooter

Problem with static function

I have an issue with my code. I am trying to access some static functions but some things are not working properly in the compiler.

 static function OnTriggerEnter (other : Collider) {
      if (other.gameObject.tag == "ball") {        
         
         Instantiate (explosion, transform.position, transform.rotation);        
         Destroy(other.gameObject);
         Destroy(gameObject);                
         score +=50;
         guiScore.text = "Score: " + score;
         print("hit");
     }
 }

I get the following errors:

BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'transform'.

BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'gameObject'.

Can someone please show me problem?

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
1

Answer by sparkzbarca · May 23, 2013 at 12:22 AM

that is not a static function.

remove the keyword static

it makes no sense in that context.

mark if answered please

Comment
Add comment · Show 2 · 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 slidecoast · May 23, 2013 at 12:49 AM 0
Share

If I remove the "static" from function, is there a way for me to call the 'score' variable in another script?

avatar image Kiloblargh · May 23, 2013 at 12:55 AM 0
Share
 var anotherScript : SomeOtherScript;
 
 anotherScript.score += 50;
avatar image
1

Answer by Lairinus · May 23, 2013 at 12:59 AM

If you want to use a function across different scripts, use either SendMessage or GetComponent. I know that for at least static classes, they can't derive from Monobehaviour, which means all of the nice functions / types that come from Monobehaviour is non-existent.

If you wish to use a static function however, you could use an instance variable.

For example:

 public class pseudoCodeClass : Monobehaviours
 {
  static var instance : pseudoCodeClass;
 
 function Awake()
 {
 instance = this;
 }
 }

(If the syntax is wrong I apologize, I use C#)

From this point on, you can reference the class directly:

pseudoCodeClass.instance.YOURFUNCTIONHERE();

Doing it this way is good and bad; good because you can use all of the functions and variables (Even ones using Monobehaviours!), and bad because it limits the "instance" aspect of the script.

I never use static unless I know that I have only one of the object (such as a main camera or main (only) character).

GetComponent would be a lot better if you have more than 1, such as a generic enemy unit or whatnot. However if this is a GameMaster script, then yes, this would work.

ALSO, because the instance variable makes everything static, you don't have to do anything special to your functions. You could simply make public functions and call them as static through the variable, as shown above.

Hope this helped a little bit. I'm far from a master but I spent a few too many hours on a similar issue.

-If you need anything clarified just ask... I'll see what I can do.


Re-reading your question a little bit, it looks like I missed some of it.

Try marking your variables as Static and see if that helps at all.

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

15 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

Related Questions

Multiple Colliders Trigger problem 2 Answers

OnLevelWasLoaded script problem. 1 Answer

Check if collider is colliding with anything non specific? 1 Answer

Destroying Game Object On MouseButtonDown + Distance Collision 0 Answers

how to move an object clone to mouse position, i dont understand ? 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