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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Cow dude · Aug 05, 2013 at 11:23 PM · c#static-function

Error when trying to access another script?

so i have an enemy in a room and i only want the enemy to move if the player is in the room. i added a trigger to the floor of the room and added this script to it: using UnityEngine; using System.Collections;

 public class trigger : MonoBehaviour {
     
     Transform minotaur;
 
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void OnTriggerEnter (Collider Player) {
         if (Player.CompareTag("Player")){
             attack.Attack();
         }
     }
 }

i have the attack script on my enemy: using UnityEngine; using System.Collections;

 public class attack : MonoBehaviour {
     
     public Transform targert;
     
     // Use this for initialization
     void Start () {
     //    Transform me = transform;
     }
     
     // Update is called once per frame
     static void Attack() {
         animation.Play ("intimidate");
         transform.LookAt(target);
     }
 }

i then get this error on the lines: animation.Play("intimidate"); and transform.LookAt(target);

the error is:`error CS0120: An object reference is required to access non-static member UnityEngine.Component.animation'

any idea how to fix this?

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 nixcs2512 · Aug 05, 2013 at 11:50 PM 0
Share

If you want it to work, fix these:

 //attack script
 ...
  public void Attack(){//remove "static"
  animation.Play("itimidate");
  transform.LookAt(target);
 }
 //trigger script
 ...
 void OnTriggerEnter (Collider Player) {
  if (Player.CompareTag("Player")){
  Player.GetComponent<attack>().Attack();
  }
  }

I don't know much about "static" so hope someone will explain it to you later. Thanks to @Joyrider,i edited it.Forgot the most important thing.

avatar image Cow dude · Aug 06, 2013 at 12:02 AM 0
Share

i got these errors on the attack script: Assets/maybe ai/attack.cs(16,34): error CS0103: The name target' does not exist in the current context error CS1502: The best overloaded method match for UnityEngine.Transform.LookAt(UnityEngine.Transform)' has some invalid arguments

Assets/maybe ai/attack.cs(16,27): error CS1503: Argument #1' cannot convert object' expression to type `UnityEngine.Transform'

avatar image Joyrider · Aug 06, 2013 at 12:19 AM 0
Share

you wrote

 public Transform targert

in your declaration... don't know if it is a typo from writing it here, or if it is really in your code..

avatar image nixcs2512 · Aug 06, 2013 at 07:04 AM 0
Share
  public Transform targert;
 ...
  transform.LookAt(target);

Do you know where the 1st error comes?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Joyrider · Aug 06, 2013 at 12:11 AM

Static makes it so a function can directly be called from the classReference without the need of an object for it to be called from (target).

A static function is common to all objects, just as is a static variable, hence there being no need for an object to call the function. You just use ClassName.FunctionName.

You could for example use a static variable for the time of day. Since everything in your game is at a specific time, and you can easily access it though let's say TimeClass.timeOfDay

In your example, if the attack function were static, all enemies from your world would go into attackmode, not just the one in that particular room. On top of that, since a static function does not belong to a specific object, you cannot just call animation and transform, as both get their respective components on an object the function belongs to (which again doesn't exist here, it being static). Hence your errors.

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 Cow dude · Aug 06, 2013 at 12:18 AM 0
Share

how would you suggest i fix it?

avatar image Joyrider · Aug 06, 2013 at 12:23 AM 0
Share

nixcs2512's solutions seems the way to go

avatar image Cow dude · Aug 06, 2013 at 02:06 AM 0
Share

i did but i got this error:error CS0122: `attack.Attack()' is inaccessible due to its protection level

avatar image Joyrider · Aug 06, 2013 at 06:41 AM 0
Share

All you had to do to remove that error was add public before void Attack()

 public void Attack()
 {
 }

Because à function is private by default (and can thus externally not be called upon by other scripts)

avatar image
0

Answer by RyanZimmerman87 · Aug 06, 2013 at 12:53 AM

I recently started messing around with trying to find improved methods for activating functions from other scripts and how to call static functions.

Traditionally I would use something like this:

 //example of accessing another script to start boss battle music
 
 CameraScript otherObjectsScriptCamera = cameraObject.GetComponent<CameraScript>();
 otherObjectsScriptCamera.levelMusicFunction();
 

However this requires that you have a cameraObject declared for you script to access that object component, sort of annoying when you might have to do this kind of thing hundreds of times for various functions.

I recently discovered a new method which I'm considering replacing all the kind of stuff above with something like this instead:

 public class PlayerXPScript : MonoBehaviour 
 {
 
 //creates a script variable so that you can access static functions
 //I believe this is the exact type of thing you need to do for that error your posted
 public static PlayerXPScript XPInstance;
 
 //creates a function for other objects to access globally
 //this function just activates the real function but eliminates that error message you posted.
 public static void XPUpdateCall()
 {
 XPInstance.XPUpdateFunction();
 }
 
 //the actual function 
 void XPUpdateFunction()
 {
 //real function that can use non-static field, method, or properties 
 }
 
 }
 
 }

This set up allows you to bypass the errors "An object reference is required"

So in this example all enemies would be able to call the playerXPUpdateFunction with this:

 PlayerXPScript.XPUpdateCall();

Instead of:

 PlayerXPScript otherObjectsScriptXP = playerObject.GetComponent<PlayerXPScript>();
    
 otherObjectsScriptXP.XPUpdateFunction();
 

This also allows you to not use an object for each enemy to easily gather that component from the player.

I don't know why it took me so long to realize to try this, but from my understanding it should be a significant improvement allowing static functions to operate flawlessly by calling global static functions.

However, this is not really good for your specific problem because you want to call an individual monster not a global function.

For that I would suggest removing the static variable and accessing it like my first example. Although you could also set the trigger onto the enemy instead of the floor of the room. So when the the player is in a certain range of the enemy it calls the function. Not sure if that would work for your project though.

But hopefully this answer helps you a little, I'm still relatively new to programming so I am very interested to find better ways to handle this kind of stuff.

Comment
Add comment · Show 3 · 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 Cow dude · Aug 06, 2013 at 04:35 AM 0
Share

this got rid of my errors but now when i enter the trigger i get Object reference not set to an instance of an object for your line 12

avatar image Joyrider · Aug 06, 2013 at 06:39 AM 0
Share

If you literally took over Ryan's code and have that error, my guess is XPInstance is null, you still need to get the playerObject and put it into XPInstance.

avatar image RyanZimmerman87 · Aug 08, 2013 at 07:22 PM 0
Share

Whoops I don't know how I forgot that possible problem.

So if you have the code like my example:

  //creates a script variable so that you can access static functions
     //I believe this is the exact type of thing you need to do for that error your posted
     public static PlayerXPScript XPInstance;
     
     //then you can do something like this to solve the XPInstance being null really sorry I forgot this info.
     
     void Start()
     {
         XPInstance = GetComponent<PlayerXPScript>();
     }

Then when you call the function from the enemy with:

 PlayerXPScript.XPUpdateCall();
 

You will be able to have a bit more options and flexibility with the resulting functions:

 //creates a function for other objects to access globally
 //this function just activates the real function but eli$$anonymous$$ates that error message you posted.
 public static void XPUpdateCall()
 {
 XPInstance.XPUpdateFunction();
 }
  
 //the actual function
 void XPUpdateFunction()
 {
 //allows you to use a wide variety of static variables even though called indirectly by another Objects Script.
 //calls other functions easily
 }

I'm still so new to program$$anonymous$$g I'm having trouble describing how this kind of set up seems like it could be extremely useful. But it allows the enemies to call the function with the one line of code:

The above function can include a wide variety of function calls and static variables which seemed to be otherwise impossible to call from my previous methods when called from another gameObjects Script.

I wish I could explain it better but it does seem to have some useful applications.

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Static Function Aliases? 4 Answers

Can't move character. C# 1 Answer

error CS0118: `New_Career.Fame' is a `field' but a `type' was expected 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