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 Salmjak · Nov 25, 2014 at 08:37 AM · gameobjectinheritance

Do game objects "inherit" the classes of their script components

Ok, so here it is. I'm not sure I've grasped what the "script components" really do. I've created my own class that all "mobs" (monsters, players, you name it) should belong to. I want to visualize the mobs with a, let's say, 3D object. So I create the object and attatch the script containing the script with public/serialized variables that I can change for each individual 3D object, right?

Below is the code for my class. But how would I pass the 3D object into a function like "void Greet(MobInfo mobInfo)"?

To sum it up: 1) Are the variables I assign in the inspector tab SPECIFIC for that individual 3D object? (or do I have to add some code saying "MobInfo player = gameObject" with an individual subclass for each 3D object)

2)If 1) is true, how would I carry on and retrieve the 3D object as a member of the MobInfo class.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MobInfo : MonoBehaviour {
 
     public bool isPlayer;
     [SerializeField]
     public Dictionary<string, int> status = new Dictionary<string, int>() //MAIN STATS --- NOTE!!! These should only be changed when recieving a PERMANENT bonus
     {
         {"HP", 50},
         {"MP", 50},
         {"STR", 10},
         {"DEX", 10},
         {"MAGIC", 10},
         {"EVASION", 1},
         {"DEFENSE", 10},
         {"SPEED", 1}
     };
     public string currentMove;
     [SerializeField]
     public Spell currentSpell;
     public bool isTurn;
     private string[] moves = {"Attack", "Defend", "Spell", "Run"};
     private bool showGUI;
     public Dictionary<string, int> ModStats = new Dictionary<string, int>(); //Used to store equipment bonus or (de)buff effects (temporary changes)
     public Dictionary<string, int> currBuffs = new Dictionary<string, int>(); //Will reflect the remaining duration of a (de)buff
 
 
     public void ChooseMove() {
         if (isPlayer) { //If it's the player, show the GUI
             showGUI=true;
             currentMove = ChoiceGUI ();
                 } else { //If it's the computer, make a random choice
             int rand = Random.Range(0,2);
             currentMove = moves.GetValue (rand).ToString();
                 }
     }
 
     public string ChoiceGUI() {
                 if (showGUI) {
                         GUI.Box (new Rect (10, 10, 100, 140), "");
     
                         if (GUI.Button (new Rect (20, 40, 80, 20), "Attack")) { //MAKE ATTACK BUTTON
                                 showGUI = false;
                                 return "Attack";
                         }
 
                         if (GUI.Button (new Rect (20, 70, 80, 20), "Defend")) { //MAKE DEFEND BUTTON
                                 showGUI = false;
                                 return "Defend";
                         }
                         if (GUI.Button (new Rect (20, 100, 80, 20), "Spell")) { //MAKE SPELL BUTTON
                             showGUI = false;
                             return "Spell";
                         }
                         if (GUI.Button (new Rect (20, 130, 80, 20), "Run")) { //MAKE RUN BUTTON
                             showGUI = false;
                             return "Run";
                         }else {
                             return "Error";
                         }
                 } else {
                         return "Error";
                 }
 
         }
 
 } 



Comment
Add comment · Show 2
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 gjf · Nov 25, 2014 at 09:27 AM 1
Share

you're right - you haven't really grasped how they work ;)

variables are specific to an instance of the script component attached to a GameObject unless they're marked as static.

you add this script component to each object and to access it's variables use GetComponent to get a reference to that script and its variables.

you might consider using a class/struct ins$$anonymous$$d of a Dictionary too, unless there's a specific reason you chose it...

avatar image Salmjak · Nov 25, 2014 at 12:44 PM 0
Share

Thanks! :) I realize how they work now (I hope) and for the first time really understand what static "really means" :P

So basically, all gameobjects with the script "$$anonymous$$obInfo" will have it's individual variation (instance) of that script run and I should be able to pass on these variables as "$$anonymous$$obInfo NPC = gameObject.GetComponent();" and then shove NPC into a function, e.g. void Greet($$anonymous$$obInfo mob)? :) The function Greet(NPC); would then access variables from the instance of the class $$anonymous$$obInfo that is associated with the gameObject, right?

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

how to deal with GameObject class and my custom inheritance 1 Answer

Adding script during runtime 2 Answers

Prefab variants inherited scripts 1 Answer

Lines of code playing multiple times? 1 Answer

How unity works under the hood? 0 Answers


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