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 Tyler 2 · Apr 17, 2011 at 03:14 AM · errorgetcomponentstatic

Accessing components/variables?

Hello. I am trying to use GetComponent to access public variables that are in another script, but I keep getting errors(such as "An instance of type 'UnityEngine.Component' is required to access non static member 'GetComponent'.").

I want to make a pokemon style turn based RPG. I am going to have all of the available moves that the monster can do be functions and store those functions a single Moves_Database script. Then, when a monster goes to execute a move, that move's function would be called, but it would then be modified by the stat values inside that particular monsters script.

he way he battle system works, is first the script scans the scene for all objects with the enemy tag (which all have the "Enemy" script attached), then, targets a specific enemy, then it it accesses that enemy's specific script (the enemy's stats are public variables), and then, for the attack, it enacts some sort of modifier on those stats (subtracting HP, decreasing agility, etc.).

Here is the the one that invokes the other script's function(its called B_Controlled)

public var Monster_List :GameObject[]; public var Monster_SLCT =5; public var Attacks_SLCT : String;

function P1 (){Monster_List=GameObject.FindGameObjectsWithTag ("Enemy");} function P2 (){Monster_SLCT = 10;}
function P3 (){Attacks_SLCT = "THUNDER";} function P4 (){Invoke(A_Moves.Attacks_SLCT);}

And here is the moves database (A_Moves)

static function THUNDER (){ //This is the part of the function that finds the enemy that the B_controlled script found. GetComponent("B_Controlled").Monster_List[GetComponent("B_Controlled").Monster_SLCT]

//and this part affects the stats of the Enemy script .GetComponent("Enemy").HP-=1; }

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 Jesse Anders · Apr 17, 2011 at 04:57 AM 1
Share

Just to make the question a little easier to answer, perhaps you could edit your post to include the exact error message, and the specific statement in your code that generates the error message.

1 Reply

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

Answer by loopyllama · Apr 17, 2011 at 01:36 PM

GetComponent is really Component.GetComponent in your script, where Component is the instance of the class. Your function is static whichs means it cannot access any instances of the class. This is why you get an error message saying you need an instance of Component in order to call GetComponent. Make your function public rather than static, then you can access instances and call the function from other classes.

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

No one has followed this question yet.

Related Questions

Disabling a Script with C# 3 Answers

Why am I getting this error when accessing a variable or function on my script using GetComponent - 'x' is not a member of 'UnityEngine.Component'? 1 Answer

GetComponent Editing Variables not working in 3.5? 0 Answers

Beast Lightmapping is not working just when he wishes :)) every time im getting errors 5 Answers

NullReference Problem with pragma strict 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