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 Dusinn · Mar 14, 2011 at 05:26 AM · gameobjectcomponentrpgaccessing

Activate objects/crafting in a game.

So, i was wondering how i should do this. Lets say I have 3 different boxes, one represent a anvil, one a cooking pot, and one a saw. I want to get my toon to do different things when i activate them. I would prefer to walk up to them and press 'F' and it would activate a script on the box i selected. But when i think about it, all the scripts need to have the same name, and if they have the same name they must be the same script right?

So the only solution that comes to mind is to have a long script with alot of if-statements (attatched to my char), but that doesn't seem so effective, and i want NPC's to be able to use them too.

Any suggestions of alternative routes to solve my problem?

Thanks in Advance /Dusinn

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

1 Reply

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

Answer by Lab013 · Mar 14, 2011 at 07:53 AM

Either have the scripts inherit from the same superclass with a function (example)

class ScriptA {

 function InheritedFunction(value : int) : boolean {
     return false;
 }

}

class ScriptB extends ScriptA {

 function InheritedFunction(value : int) : boolean {
     return (value % 2);
 }

}

class ScriptC extends ScriptA {

 function InheritedFunction(value : int) : boolean {
     return true;
 }

}

Then you could have a handler script also that would be attached to the object you want to be clicked/have the function called on. It'd look like this

var ascript : ScriptA;

function InheritedFunction(value : int) : boolean { return ascript.inheritedFunction; }

You would then call it like this

print(gameObjectThatYouClicked.GetComponent(Handler).InheritedFunction(someValue));

or you could use the SendMessage function.

Comment
Add comment · Show 1 · 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 Dusinn · Mar 14, 2011 at 10:34 PM 0
Share

Thank you, gonna try this out But does the script that i activate know, if he (the object) is an anvil or a stove? Or should i have a list of possible objects, that translates into a number that i send to the handler script (and then it choose the correct function to call)?

Or is it possible to like: Name the scripts Crafting_001, Crafting_002 etc, and then try to call them by GetComponent("Crafting_*"), or something like that..? (could be buggy if more than one crafting script is attached to the GameObject)

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

No one has followed this question yet.

Related Questions

How do you place an image on an object so that the object size is based on pixels per unit and image size? 1 Answer

How to mimic Unity's Components based System 1 Answer

How to Check if a component Exists on a Gameobject. 3 Answers

Storing component of instance in an array 2 Answers

Unity cannot find component of an object in an array. 2 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