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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by PorkMuncher · Mar 25, 2014 at 09:51 AM · instantiateclassinventory

Unity script system

I am familiar with the basic concepts of OOP and do not understand where a "script" fits into this system.

I am trying to create an inventory system.

I was thinking of creating a class for all items, then subclass for weapons, that have GameObjects for attributes - the physical ingame objects, if I had to display them.

How would I implement such a thing? I am supposed to attach scripts TO GameObjects?! I am really confused right now ... How would I instantiate the Item class? Do I need them to be GameObjects with transforms?? Does my question even make any sense?? WHAT IS GOING ON??

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

2 Replies

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

Answer by PorkMuncher · Mar 25, 2014 at 12:49 PM

I think the confusing factor was that not every class has to extend MonopBehavior.

Im sorry that I couldn't formulate it better...

This question actually sums it up:

http://answers.unity3d.com/questions/192898/is-all-scripting-gameobject-based.html

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
avatar image
0

Answer by haim96 · Mar 25, 2014 at 10:11 AM

you probably skipped that: http://unity3d.com/learn/tutorials/modules/beginner/scripting

but make sure you check the rest of them here: http://unity3d.com/learn/tutorials/modules

Comment
Add comment · Show 5 · 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
avatar image PorkMuncher · Mar 25, 2014 at 10:30 AM 0
Share

There is a key philosophy that I don't understand. The begginer tutorials just don't deal with it. I don't even know what the appropriate question here is.

Can I create several classes in one script? $$anonymous$$y only OOP experience so far has been in Java, so I have never even considered this.

I will do more research for sure, but so far answer to my own questen seems to be something like this:

You make GameObject - a handler - to have a script that has the item class in it. The script creates instances of item class and stores them. So it all gets to be under a GameObject, but not every instance is a GameObject instance.

avatar image PorkMuncher · Mar 25, 2014 at 11:04 AM 0
Share

Could the actual answer be:

When attaching a script to a GameObject, it simply means the object has an INSTANCE of the script as parameter.

Now I am confused again. Why do I have to attach scripts to other objects. OR DO I?

avatar image haim96 · Mar 25, 2014 at 11:59 AM 0
Share

http://unity3d.com/learn/tutorials/modules/beginner/scripting/scripts-as-behaviour-components

the answer is in the title: Scripts as Behaviour Components you attach script code to define the Behaviour of a gameobject.

in theory, you could write a complete game in single script but it wan't be efficient way to do so.

avatar image haim96 · Mar 25, 2014 at 12:06 PM 0
Share

if you want real life example, consider a script that make a bullet fly. you could use findgameobject to find all bullets in scene and make them fly one by one. a better way will be attach each bullet gameobject a script that will make him fly. no?

avatar image trololo · Mar 25, 2014 at 01:17 PM 0
Share
  • A GameObject always has a transform component

  • A script attached to a GameObject is defined as a component

  • A script need to have a class which extend $$anonymous$$onoBehaviour to be defined as a component

  • But a script can contain other usual C# class

  • A script can reference others GameObject in scene by referencing them in public attributes (it has to be set in the inspector before the run time) or by finding them with GameObject.Find()

  • You can get a component attached to a GameObject by using the function: yourGameObject.GetComponent()

  • To add the script to a GameObject just drag & drop your file into it or, during the run time, use: yourGameObject.AddComponent()

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create an instance of a class stored in another script 0 Answers

Calling an object from a different JS Script and Instantiate it 1 Answer

making prefabs at runtime? 2 Answers

Calling a Function from other script ( C# ) 1 Answer

OnTriggerEnter / Inventory question?! 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