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 /
avatar image
0
Question by madmike6537 · Dec 15, 2012 at 12:04 AM · getcomponentfunction

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.

Comment
Add comment · Show 8
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 bompi88 · Dec 15, 2012 at 12:53 AM 0
Share

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);?

avatar image madmike6537 · Dec 15, 2012 at 04:03 AM 0
Share

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'.

avatar image madmike6537 · Dec 15, 2012 at 04:09 AM 0
Share

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.

avatar image madmike6537 · Dec 15, 2012 at 04:16 AM 0
Share

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?

avatar image whydoidoit · Dec 15, 2012 at 07:13 AM 1
Share

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).

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by madmike6537 · Feb 09, 2013 at 05:03 AM

This was answered in the comments.

Comment
Add comment · 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

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

11 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

Related Questions

GetComponent Picking up multiple scripts from differrent objects? 1 Answer

Issue with calling a function from another script on trigger. Using JavaScript. 1 Answer

Send Message? 1 Answer

Access function from another script (Main Camera Script) 0 Answers

Call void for one Object 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