Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Jul 02, 2015 at 10:34 PM by DaiMangouDev for the following reason:

Other

avatar image
0
Question by DaiMangouDev · Jun 07, 2015 at 06:18 PM · c#gameobjectscene

How to add a script to an gameobject when it is instanced in the scene view ?

How do I go about adding a script component to any gameobject instanced in scene view .

i.e If i create a new Cube or a point light, a particular script will get attached to it .

I ave been looking at AssetPostprocessor but i did not find an answer there . the closest thing was using ModelImporter to get between the import process of models . but this will only work for models , models imported into unity.

Comment
Add comment · Show 3
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 fafase · Jun 07, 2015 at 07:41 PM 0
Share

Those you mention get component because they are prefabs. So are you needing prefabs?

avatar image DaiMangouDev · Jun 08, 2015 at 01:46 AM 0
Share

prefabs are not the main concern now . I'm mean that is i click GameObjecy/3D Object/Cube and instance a cube , I want a particular script (custom script) to be added to it.

sorry if i'm being too unclear or not understanding something

avatar image Ricewind1 · Jun 08, 2015 at 01:49 AM 0
Share

You can either add a script manually, by code, or by using a prefab. To my knowledge there is no way to automatically attach a script to a freshly instanced cube. I don't really see the need either since that's what prefabs do

1 Reply

  • Sort: 
avatar image
0

Answer by pako · Jun 07, 2015 at 07:24 PM

You can use GameObject.AddComponent:

http://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html

For example when the following script is attached on GameObject1 in the scene, when it is clicked, it will first create a cube primitive in the scene, and then add a CustomMonobehaviour to it:

 public class AddBehaviour : MonoBehaviour {
 
     // Use this for initialization
     void OnMouseDown() {

          GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube)
          cube.AddComponent<CustomMonoBehaviour>(); 

     }
 
 }

This seems to be what you want.

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 DaiMangouDev · Jun 08, 2015 at 01:43 AM 0
Share

I meant that; when if i click GameObjecy/3D Object/Cube and instance a cube , I want a particular script (custom script) to be added to it.

sure this method would work but than i could just drag the scripts on the objects too.

avatar image pako · Jun 08, 2015 at 07:02 AM 0
Share

I only had to change Start() to On$$anonymous$$ouseDown() in my answer to adjust it to your new info. Now, whenever the GameObject/3D Object/Cube with this script gets clicked to instance a cube, it will add the custom script e.g. "Custom$$anonymous$$onoBehaviour.cs" to the instanced cube.

I use a public variable 'go' as the gameObject where the custom script is added, and also I don't show the instantiation code, because this is an example. In your actual code, you just use the variable that holds an instance of the GameObject you instantiate, ins$$anonymous$$d of 'go' in the example.

avatar image pako · Jun 08, 2015 at 07:10 AM 0
Share

I edited my answer, yet again, to include cube creation code as well, just in case my answer was still not clear.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Distribute terrain in zones 3 Answers

Readd GameObject to scene (to restore it) 0 Answers

Find GameObject in another loaded scene 2 Answers

How Do You Have Multiple High Scores For 1 GameOverScene? 1 Answer

Parent-child scaling problem (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