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 Cora_and_Clarice · Jul 16, 2014 at 01:08 PM · c#inheritance

How can I programatically assign one script as a variable inside another script for use by derived classes?

In my game, I have a bunch of objects the player can interact with to varying degrees. I have implemented this in C# scripts using the following general hierarchy:

// to define basic methods for looking at, picking up etc.
public abstract class Interactible: MonoBehaviour

// a concrete Interactible
public class SomeObject: Interactible

// to define an extra method Use() for objects that can be used on something else later on
public abstract class Usable: Interactible

// concrete usables
SomeSpecialObject: Usable
SomeOtherSpecialObject: Usable

I have another script called ObjectManager that takes responsibility for displaying the interaction interface to the player, depending on how the player is currently relating to the object and what type of object it is. e.g.1 if they've just walked up to it, it displays an image of the xbox controller button mapping to pick up e.g.2 if they've already picked it up, it displays the buttons mapping to use or drop etc

I want every object to have an objectmanager script. So I want to create it once as a protected variable in Interactible and never have to worry about it again.

How can I programatically assign a script as a variable inside another script? Does the fact that the base script in which I want to do this is abstract and not attached to any game objects itself change anything?

I've tried:

i) declaring the protected script variable inside Interactible and calling GetComponent() in the Awake() method, but Unity says it is not instantiated when I try to use it in the concrete derived classes later on in their update methods.

ii) putting [System.Serializable] above the class definition for all classes in this hierarchy in an attempt to force it to appear in the editor. This would mean I'd have to manually drag the script onto every object in the editor, but at this point I'm just trying to get it to work at all. However, the protected variable is never appearing in the editor.

I'm very new to the Unity environment and I suspect there's something wrong with my understanding of how to componentise my code. Any help much appreciated!

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 fafase · Jul 16, 2014 at 01:12 PM

 public abstract class Interactible: MonoBehaviour{
    protected NeededScript neededScript;
 
    protected virtual void Start(){
       neededScript = AddComponent<NeededScript>();
    }
 }

If you have a sub class that is also using Start, you need to call up the base one:

 public class SomeObject: Interactible{
    protected override void Start(){
       base.Start();
       // Other things
    }
 }

or you can simply check if the component is not null:

 public class SomeObject: Interactible{
    void Start(){
       if (neededScript == null) neededScript = AddComponent<NeededScript>();
    }
 }

But the previous is better, at least to me.

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 Cora_and_Clarice · Jul 17, 2014 at 08:25 AM 0
Share

Okaaay! AddComponent not GetComponent, thanks!

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

An OS design issue: File types associated with their appropriate programs 1 Answer

Multiple Cars not working 1 Answer

Values lost? 1 Answer

Calling a method of a class that is a part of another class? 1 Answer

Same script on multiple game objects [solved] 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