- Home /
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?
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.
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'
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..
public Transform targert;
...
transform.LookAt(target);
Do you know where the 1st error comes?
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.
i did but i got this error:error CS0122: `attack.Attack()' is inaccessible due to its protection level
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)
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.
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
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.
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
Follow this Question
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