- Home /
Trying to call PlayerInventory.Add(); from outside my script - problem.
So I have an item, and I am trying to add it to my list (aka my inventory). But I cant quite figure out how to call the add function from my script that has the player clicking on the object. Let me back up a step and explain how I have it set up as thats part of the problem:
I have some scripts for inheritance which are designed to classify what kind of object that the item is. This particular item, an arrow, is going to be listed as a Projectile, so I have a script, Projectile which extends a base Item script:
public class Projectile : Item
Now, on a seperate script thats attached to my arrow prefab, I have the following code:
private string itemName = "Arrow";
void Start ()
{
Projectile myArrow = new Projectile();
myArrow.name = itemName;
}
What I want to happen next is in my script on my player that says "if I click on the arrow", I want to use PlayerInventory.Add(myArrow);
Problem is, this doesnt work. Basically, myArrow listed above, is not public. I can call the Add function inside the Arrow script, but I cant seem to call it anywhere else.
So my questions are:
How can I call PlayerInventory.Add(myArrow) from outside the arrow script, thereby adding my arrow into my player inventory? Can I make this information public somehow?
Question 2: Does this sound like even the right way to go about all this? Does the way I am setting up scripts for inheritance to classify an item as a "Projectile" for example sound like its making sense?
Thanks a bunch in advance.
Ok, first off, you are actually classifying the projectile as an item, and that's a logical way of doing it. ;-) You wan't the projectile to have all the properties that an item has, right? So you are doing it right. If you wan't to make the projectile variable public, you could do:
private string itemName = "Arrow";
public Projectile myArrow;
void Awake ()
{
myArrow = new Projectile();
myArrow.name = itemName;
}
And sorry, I didn't quite understand where you want to call PlayerInventory.Add(myArrow);?
Thanks for the reply! :)
So I I have updated my script as you listed your reply. Now in a seperate script which holds all my player actions (its called player input) I have a raycast, and if it hits my arrow prefab, then I call this:
itemScript.PlayerInventory.Add(myArrow);
However, I am getting an error that it has some invalid arguments. It also says, cant convert 'Arrow' expression to type 'Item'.
Wait never$$anonymous$$d - I figured it out. I need to rename my variables lol - I actually had another myArrow in that script as well, so I needed to call myArrow.myArrow lol. Guess I need to rethink those names. Can you answer one final question - and then put it as an answer if you want so I can give you kharma! :)
$$anonymous$$y question is this:
So on all my items as I am creating them, I just need to put a script and then put public Projectile(or whatever it might be) myItem;
and then say myItem = new Projectile() (or again whatever it might be)
and by doing that, it will actually set that item to be listed as a type Projectile/whatever? Could I then do, If(myItem.GetType == projectile) // do stuff? Would that work? I am trying to figure out a good way to click on an item, and then figure out what it is so I can add it to my inventory GUI.
One other question: Sorry - I am also getting a warning - 'You are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword. This is not allowed. $$anonymous$$onoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all'
It references the myArrow = new Projectile(); line. Any ideas on why it would say that?
Right so a Projectile is an Item which is a $$anonymous$$onoBehaviour?
So in Unity a thing which is a $$anonymous$$onoBehaviour cannot exist without a game object to go with it. You can't just new SomethingDerivedFrom$$anonymous$$onoBehaviour() because that doesn't make a game object or associate the behaviour with one. Also all $$anonymous$$onoBehaviours are forever tied to the game object that they are created with.
So creating a $$anonymous$$onoBehaviour looks like this if you need a new game object:
var go = new GameObject("Arrow");
var arrow = go.AddComponent<Projectile>();
arrow.name = "Whatever";
Or you can add it to an existing object using gameObject.AddComponent()
Now perhaps Projectile/Item etc shouldn't be $$anonymous$$onoBehaviours - it depends if they are actually moving something about etc. Perhaps they should be ordinary classes or classes derived from ScriptableObject (which allows you to make custom assets from them).
Your answer
