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 2Tie · Mar 22, 2011 at 11:38 PM · instanceattach

Create an object and assign code to it via script?

I was wondering how it would be possible to create an instance of a prefab and assign a character controller and scripts to it by a code file (preferably in javascript). In my case, the prefab is named Smiley4, and there are two scripts that need to be placed on it when it is instantiated.

thanks in advance, 2Tie

Edit: Mah, i should probably add my current code for it: var NewModel : GameObject; var P1Charname:String = "Smiley4";

function Start() { Instantiate(NewModel, transform.position, transform.rotation); GameObject.Find(P1Charname+"(clone)").AddComponent(CharacterController); GameObject.Find(P1Charname+"(clone)").AddComponent("Gui_StatDisplay"); GameObject.Find(P1Charname+"(clone)").AddComponent("P1Script"); } Smiley is correctly instantiated, but the scripts aren't added...

Comment
Add comment · Show 2
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 AngryOldMan · Mar 23, 2011 at 12:04 AM 0
Share

could you put all your scripts into one and have that set as standard on the prefab?

avatar image AngryOldMan · Mar 23, 2011 at 12:04 AM 0
Share

all scripts applied to the player I mean!

1 Reply

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

Answer by efge · Mar 22, 2011 at 11:54 PM

You could use GameObject.AddComponent


Edit:
Your script could look like this:

var NewModel : GameObject;

function Start() { var clone : GameObject; clone = Instantiate(NewModel, transform.position, transform.rotation); clone.AddComponent("CharacterController"); clone.AddComponent("Gui_StatDisplay"); clone.AddComponent("P1Script"); }

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 2Tie · Mar 23, 2011 at 12:00 AM 0
Share

The code I've set up right now goes var New$$anonymous$$odel : GameObject; var P1Charname:String = "Smiley4";

function Start() { Instantiate(New$$anonymous$$odel, transform.position, transform.rotation); GameObject.Find(P1Charname+"(clone)").AddComponent(CharacterController); GameObject.Find(P1Charname+"(clone)").AddComponent("Gui_StatDisplay"); GameObject.Find(P1Charname+"(clone)").AddComponent("P1Script"); } It instantiates the object, but nothing is added.

avatar image efge · Mar 23, 2011 at 12:02 AM 0
Share

edited my answer

avatar image 2Tie · Mar 23, 2011 at 12:19 AM 0
Share

Thanks! That helped a lot!

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 to add new instance of a script to a game object? 2 Answers

How do i write a script that switches between 2 variables. 1 Answer

Script in UNITY ??? 2 Answers

Character Controller Jittering Problem 2 Answers

Why isn't this script working? (Beginner in coding) 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