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 /
avatar image
0
Question by IdRatherBeSailing · Apr 13, 2020 at 11:32 AM · scripting beginnereventscriptable objectinteractableevent handlers

How to add an Interactable script component to a GameObject programmatically

For a MS Hololens app, I've created a cube GameObject in Unity, with a simple Interactable script component that changes the cubes color depending on focus:

alt text

This is the full interactable component for the cube in Unity: alt text

I'm now creating the cube objects through a script:

  void Awake() 
     { 
         // Create a cube at the origin 
         cube = GameObject.CreatePrimitive(PrimitiveType.Cube); 
         cube.name = "CUBE01"; 
         cube.transform.position = new Vector3(0.172f, -0.112f, 3.722f); 
         cube.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f); 
     }

How do I even begin to add the same interactable script component in the code for the cube? Using an EventListener of some kind?

focus.jpg (77.8 kB)
annotation-2020-04-13-212120.png (100.5 kB)
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
1
Best Answer

Answer by Casiell · Apr 13, 2020 at 12:27 PM

Use AddComponent method if you really want to create this cube through code.

But I would honestly recommend using prefabs for that, it's the exact tool for the job

Comment
Add comment · Show 2 · 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 IdRatherBeSailing · Apr 14, 2020 at 11:50 AM 0
Share

Very interesting! Thank you, that was a great help and solved the problem. I've had to change my thinking a little with Unity.

For anyone else reading this, prefabs are a little tricky but definitely a much better solution that coding the whole thing. I created a GameObject Cube, added the Interactable script, and added the Prefab to a 'Resources' folder under Assets.

The following line was used to instantiate the object:

 GameObject cuboid = Instantiate(Resources.Load("CuboidPrefab") as GameObject, new Vector3(0,0,0), Quaternion.identity);

And when viewing the object in the HoloLens app, it does indeed call the Interactable script as per the Prefab definition.

avatar image Casiell IdRatherBeSailing · Apr 14, 2020 at 08:05 PM 0
Share

If it works in your case you can also just drag and drop prefabs to a field in your script.

So if you want script with Interactable class you declare the field as

 public Interactable interactablePrefab;

Then you drag your prefab into this field in the inspector. To instantiate just do:

 Interactable interactableInstance = Instantiate(interactablePrefab, new Vector3(0,0,0), Quaternion.identity);

This way is faster than loading from resources in terms of performance and also gives you the Interactable object outright, so you don't have to search for it with GetComponent. Doesn't work if your spawner class in not a $$anonymous$$onoBehaviour as you won't have access to inspector for this class

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

138 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 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 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

custom event delegate - convertible error 3 Answers

Help building a script to alter parameters of glitch effect in URP,Script to alter parameters for camera glitch effect 0 Answers

How do you access the editor event args? 1 Answer

Unhandled exception event doesn't trigger? 1 Answer

How to check if my player has reached a certain position? 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