Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by bern422 · Dec 23, 2015 at 09:15 PM · c#scripting problemgameobjectinheritanceinteraction

Grab an inherited subclass script from a GameObject in C#

Hey all,

New to Unity. Trying to make an interaction script that lets actions occur on RaycastHit with a GameObject tagged as "Interactable". The idea is to make a behavior script shell that works thusly:

 using UnityEngine;
 using System.Collections;
 
 public class InteractableBehavior : MonoBehaviour {
 
     // Use this for initialization
     void Start () {        
     }
     
     // Update is called once per frame
     void Update () {        
     }
 
     public void InteractWith(Camera interactor) {    
     }
 
     public void StopInteraction() {    
     }
 }

And is then extended to specify specific behavior:

 using UnityEngine;
 using System.Collections;
 
 public class BlockBehavior : InteractableBehavior {
 
     Camera holder;
 
     bool interactionActive = false;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(interactionActive) {
             transform.position = holder.transform.position+holder.transform.forward;
         }
     }
 
     public new void InteractWith(Camera interactor) {
         holder = interactor;
         interactionActive = true;
     }
 
     public new void StopInteraction() {
         interactionActive = false;
     }
 }

This way, I can just grab the GameObject from the RaycastHit:

 interactionTarget = hit.transform.gameObject;

do some step

 ????

and call interactionTarget.interactWith(playerCamera) or some equivalent. I'm just not used to C# and grabbing scripts from GameObjects. Essentially I want to specify that I'll be using a script of type "InteractableBehavior" instead of "BoxBehavior", "DialogueBehavior", etc so that the interface can be uniform. Right now I'm trying:

 InteractableBehavior interactionScript = interactionTarget.GetComponent<InteractableBehavior>();

but it's not working. I'm open to suggestions.

Hope this makes sense. Thanks in advance for the help!

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

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

48 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to enable script on another gameobject in C#? 1 Answer

Destroyed instance of Prefab, can't spawn it back. 1 Answer

I have 12 objects using the same script below, I need them to each activate individually of each other. 0 Answers

How to use IF statement ONLY when a certain gameobject is in the scene? 1 Answer

Detect overlapping objects 2D game 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