- Home /
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??
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
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
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.
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?
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.
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?
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()