• 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
2
Question by spinaljack · May 03, 2010 at 11:16 PM · arraysrpgmmoquest

Array of scripts

I've got a script on a game object and I want it hold an Array of other scripts on the same game object. I thought t$$anonymous$$s might be a good way of storing MMO style quests as each quest can have its own script and its own rules and functions.

I've tried declaring the array as Component[] and it lets me drag the scripts into the array in the inspector but it wont let me call any functions on the quest script because it says "x is not a member of component"

Is there a way to store scripts in an array w$$anonymous$$ch lets you access functions in specific scripts? I thought about using SendMessage but it would be called on every single script in the game object w$$anonymous$$ch might slow the game down if there are hundreds of quests.

Is there a better way to manage quests?

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
7
Best Answer

Answer by fherbst · Jun 15, 2010 at 09:02 AM

Your question reminded me of somet$$anonymous$$ng in my programming class - type polymorp$$anonymous$$sm! And I was curious whether t$$anonymous$$s can be used in Unity. So I took the time to put it together.

The idea is to have a basic quest class:

using UnityEngine; using System.Collections;

// BaseQuest derives from MonoBehaviour, so it can be added to GameObjects

// abstract means: t$$anonymous$$s class does not implement any functions, it only says w$$anonymous$$ch functions // must be implemented in derived classes public abstract class BaseQuest : MonoBehaviour { // here we have the basic quest behaviour - each derived class knows what to do with t$$anonymous$$s function public abstract string QuestText(); }

T$$anonymous$$s script is never actually added to any GameObject, its just the base for other quest scripts:

A simple quest:

using UnityEngine; using System.Collections;

// a simple quest is a quest - so it derives from the BaseQuest class public class SimpleQuest : BaseQuest { void Start () {}

 // the "old" QuestText method from BaseQuest gets overwritten
 public override string QuestText()
 {
     return ("I am a simple quest!");
 }

}

A more advanced quest:

using UnityEngine; using System.Collections;

// an advanced quest is a quest - so it derives from the BaseQuest class public class AdvancedQuest : BaseQuest { void Start () {}

 // the "old" QuestText method from BaseQuest gets overwritten
 public override string QuestText()
 {
     return ("I am an advanced quest!");
 }

}

Right now, you can add the AdvancedQuest and SimpleQuest scripts to a GameObject as many times as you want. Now comes the script that handles everyt$$anonymous$$ng:

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class QuestHandler : MonoBehaviour {

 // T$$anonymous$$s list handles all the quests
 private List<BaseQuest> quests;

 void Start ()
 {
     // the quests list gets created
     quests = new List<BaseQuest>();

     // collecting all the quests from the GameObject (the player)
     Component[] questsComps = gameObject.GetComponents(typeof(BaseQuest));  
     foreach(Component comp in questsComps)
     {
         // adding the quest to the list
         addQuest(comp as BaseQuest);
     }

     // each quest shows $$anonymous$$s message
     foreach (BaseQuest quest in quests)
         // here comes the magic - type polymorp$$anonymous$$sm!
         // all quests in the list derive from BaseQuest, w$$anonymous$$ch has t$$anonymous$$s function
         // all the derived quests override the QuestText function with their own behaviour
         Debug.Log(quest.QuestText());
 }

 void addQuest(BaseQuest q)
 {
     // adding the quest to the list
     quests.Add(q);
 }

}

It gets added to the same GameObject. On Start(), it collects all the scripts of BaseQuest class and all its derivates (t$$anonymous$$s was were I wasn't sure Unity would handle it in t$$anonymous$$s (right) way). All the quests get added to a quests List of BaseQuest objects.

And then every quest is asked to return its QuestText - and due to the type polymorp$$anonymous$$sm, the right QuestText method is chosen and executed.

Thanks for t$$anonymous$$s cool question!

Comment
Add comment · Show 4 · 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 _Petroz · Jun 15, 2010 at 09:21 AM 0
Share
avatar image spinaljack · Jun 15, 2010 at 03:09 PM 0
Share
avatar image fherbst · Jun 15, 2010 at 03:38 PM 0
Share
avatar image fireDude67 · Nov 26, 2010 at 09:13 PM 0
Share
avatar image
-1

Answer by Lucas Meijer 1 · May 04, 2010 at 07:07 AM

Best I can t$$anonymous$$nk off right now is to have remember w$$anonymous$$ch quests to use by their name. string[] quests. It doesn't give a great UI experience out of the box (you cannot drag quest scripts onto them), however that is a great excuse to write a custom inspector, where you can drag quests scripts onto it, w$$anonymous$$ch will then in turn just read the name from the script, and add it to the stringarray. At runtime, you can get at the .net type of your script using Type.GetType("BigAssDragonQuest");

Comment
Add comment · Show 3 · 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 spinaljack · May 04, 2010 at 09:46 AM 0
Share
avatar image Lucas Meijer 1 ♦♦ · May 06, 2010 at 07:22 AM 0
Share
avatar image spinaljack · May 18, 2010 at 10:33 AM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Looking for a chat text box that allows voice chat... 0 Answers

Unity Photon Network Problem 0 Answers

I need help fixing this code 1 Answer

How do I optimize my game online rpg become available?? 0 Answers

Creating Quests in Unity 3 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