player = GameObject.FindWithTag("Player").GetComponent(ResourceManager);
I receive numerous errors detailed below, I have used the same line in other scripts a while ago (I copied and pasted it from the other) but it does not like it this time round.
ResourceAdd.cs(10,64): error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected
ResourceAdd.cs(10,51): error CS1502: The best overloaded method match for `UnityEngine.GameObject.GetComponent(System.Type)’ has some invalid arguments
ResourceAdd.cs(10,51): error CS1503: Argument #1' cannot convert object’ expression to type `System.Type’
Everything I read in the Unity manual and scripting API reference points to the fact that I AM using it correctly, but why am I receiving these errors where I do not in other scripts, the only thing that has changed is the ResourceManager bit used to point at another script.
In C# you cannot use the name of a class as a Type parameter; which I believe you can do in Javascript. GetComponent has 3 overloads. One takes a generic type parameter, another requires a System.Type and lastly you have one that requires a string as argument.
Here is how you would write either of them:
//This is the one I prefer, as it is already casted to the correct type.
GameObject.FindWithTag("").GetComponent<ResourceManager>();
GameObject.FindWithTag("").GetComponent(typeof(ResourceManager));
//Unity even considered removing this at a point in time... so, yeah.
GameObject.FindWithTag("").GetComponent("ResourceManager");
Hmm, did you try the option: player = GameObject.FindWithTag(“Player”).GetComponent();
Presuming ResourceManager is an attached script and the tag “Player” exists of course.
That is a slight oversight in my initial answer. The variable ‘player’ is of the type GameObject, yet you try to store a component in it. Imagine you try to put an alligator in a birdcage: that won’t work (I think); yet that is roughly what you are doing here (GetComponent gives you an alligator and “player” is the birdcage). As a result the compiler will compain.
Either drop the GetComponent part, since that way you will have a GameObject to store (assuming it is not null, though that will still work… for some time). Or change the type of ‘Player’ to Component.
I have double checked just in-case and yes, the player is tagged “Player” and the script ResourceManager.cs is attached.
With the line you just gave me, I am still getting:
ResourceAdd.cs(10,9): error CS0029: Cannot implicitly convert type ResourceManager' to UnityEngine.GameObject’
EDIT: Just noticed your new post, this does somewhat make sense, but C# is not like JavaScript (UnityScript), you cannot just call a variable with no type, how should I portray the player variable then, I am trying to avoid having to drag and drop the player into a slot every time I apply this script to something?
//You could use "Component" in "ResourceManager's" stead...
private ResourceManager player;
protected virtual void Awake()
{
GameObject go = GameObject.FindWithTag("");
if (go!=null)
{
//...In that case you can drop the type casts here ("(ResourceManager)", "as ResourceManager").
//Use either of the following.
player = (ResourceManager)go.GetComponent("ResourceManager");
player = go.GetComponent("ResourceManager") as ResourceManager;
//The reason I prefer the generic version; it is shorter.
player = go.GetComponent<ResourceManager>();
}
}
I would normally post another thread about this, but it’s just a quick question, how do you reference serialized information? and how could I effect it in another script?
In ResourceManager I have this:
[System.Serializable]
public class ResourcesArray
{
public string resourceName;
public Text textDisplay;
public int resourceValue;
}
I then use:
public ResourcesArray[] resources;
In the main class to draw the contents of the serialized array, unfortunately I cannot work out how to effect these individually, I was hoping that I would be able to have wood for example as a resource name, a text field in the UI to display and value (amount) and then start the value at 0, then in the other script ResourceAdd (the one you just fixed) have another identical serialized class (without the UI Text) to allow me to for example, type in wood and the value I wantto give the player, and have the count in ResourceManager go up by however much specified in ResourceAdd when an event is triggered.
I am not sure if this is even possible, but at this point, I am just playing around, we will need a resource manager at some point, I just thought I’d take a swing at it.