|
I have been running into this issue for a while now and can't seem to figure it out. I have a player object. I also have an enemy objects that I am spawning through a prefab. I want the enemy objects to be able to access information about the player's script such as how much health it has left. how can I accomplish this in C#?
(comments are locked)
|
|
There are many many ways in unity for objects to access each other's components, scripts, functions and variables. Start reading about it at these two manual pages: Also see this answer which gives more detail about creating "dragged references": How can I access other scripts and their functions? In your particular case, if you only have a single "Player" at one time, you could use "FindObjectOfType" to find the player script instance at runtime, when the enemy is created. FindObjectOfType is ideal when you know the script you're looking for is the only one of its kind in your scene. It's relatively slow to execute, so we store the reference in a variable in Start() and just use the variable later. I'm assuming you have a script on your player, and for this example I'm going to assume it's called "PlayerScript". Whatever it's called, you need to use the exact script name where I write "PlayerScript": This would be in your enemy script:
You can then access public variables on the player's script, from inside your enemy script, using code like this:
And call functions on the player's script like this:
You can also access the Game Object to which the player script is attached:
And other built-in component references:
As well as any other component type which doesn't have a built-in reference:
(where "SomeOtherComponent" is the name of another component or script) If there is more than one object of a certain type, and you want to get references to all of them, you can use the very similarly named "FindObjectsOfType" function. This differs in that it returns an Array of the type of object that you specify. For example, if you have 10 enemies in your scene, each with "EnemyScript" attached, you could use:
You now have an array containing references to all the current instances of the enemy scripts. You can then iterate through them using a foreach loop: Thanks for the response. Is there a way to do something similar but with Objects that there are more than one of. For example check the hitPoints of an enemy?
Feb 15 '10 at 03:15 PM
sjayb2k
Yes it's possible. I've updated the answer to include this information.
Feb 15 '10 at 05:27 PM
duck ♦♦
Thanks again. you were a huge help.
Feb 16 '10 at 05:57 AM
sjayb2k
(comments are locked)
|
|
What you want to learn about is public properties in C#, getters and setters. Thanks for the quick response. I am familiar with how getters and setters work and already had a public getter for this purpose. I am getting an error: NullReferenceException: Object reference not set to an instance of an object. From my understanding that means that I haven't set my player correctly I declared the player by saying
however when I try to then set the player I get issues this line gives me the error saying I can't convert UnityEngine.GameObject to Player how can I set this correctly?
Feb 15 '10 at 08:17 AM
sjayb2k
That would be C# type casting and type conversions that you need to read about.
Feb 15 '10 at 08:22 AM
Ricardo
When I try to cast the object by saying playerCharacter = (Player)GameObject.Find("Player"); I still get an issue where it can't convert UnityEngine.GameObject to Player. Is there anything special that I have to do to declare Player as a variable in the Enemy script? It is prefab.
Feb 15 '10 at 08:37 AM
sjayb2k
The player class inherits from a class called Human. Human is the class that inherits from MonoBehaviour.
Feb 15 '10 at 09:02 AM
sjayb2k
Ah, my bad (replied early in the morning) What you're actually getting with GameObject.Find is a game object by that name. You would then need to obtain the Human component in that game object, using returnedObject.GetComponent();
Feb 15 '10 at 11:27 AM
Ricardo
(comments are locked)
|
