• 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 post has been wikified, any user with enough reputation can edit it.
avatar image
Question by NavySeal · Feb 17, 2013 at 12:50 PM · instantiatescriptingbasicsclone

Instantiate script then edit its variables

I'm new in unity and wasn't able to figure this out. My question is a bit hard to explain but i'll do my best.

I'm creating the player information struct to attach it to my prefab of player controller.

I want to instantiate the script, then change it's variables so I can set them based on the GUI and add them later to my prefab with addComponent.

But unity doesn't like "new script" I'm not really sure how to do this. I've been wandering around Instantiate() and clone tutorials but I always end up with some error.

     public CharacterChart playerChart; // (global)

     CharacterChart tempChart; // (local)
     tempChart = Instantiate(playerChart) as CharacterChart;

Currently error says the thing(clone) i want to instantiate is null, I suppose this is because I haven't initialize it.

I'm using c#

thanks

Comment
obiolg
felixmann

People who like this

2 Show 1
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 FL · Feb 17, 2013 at 02:17 PM 0
Share

A similar problem: http://answers.unity3d.com/questions/399298/design-patterns-singleton-issue.html

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by PedramAUS · Feb 17, 2013 at 03:13 PM

Instantiate is for cloning an object for instance a prefab. If you have a script, you shouldn't instantiate it, you just need to add it as a component to an object and the variables that you want to edit should be public and static, then you have access to those variables in the second script.

 CharacterChart playerChart = AnObject.AddComponent<CharacterChart>();
  
 PlayerChart.APublicStaticVariable = "A Value";
 PlayerChart.DoSomething();
Comment
obiolg
felixmann

People who like this

2 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 NavySeal · Feb 17, 2013 at 03:16 PM 0
Share

the problem is that i can only add the script as a component after I create the PController. But since the PController is only created after the script is set via GUI, I cannot add it as a component

avatar image PedramAUS · Feb 17, 2013 at 03:24 PM 0
Share

You should have a main controller in your game and protect it from being destroyed. There you should manage all these stuff such as creating an object (PController) and assigning components (CharacterChart).

avatar image NavySeal · Feb 17, 2013 at 04:49 PM 0
Share

I was able to do a "fix" on my problem but at cost that now while in GUI the playerController is active with camera and controls. Is there a way to disable the camera and movements? I cannot disable it totally because then i don't seem to be able to access values of the script inside him.

avatar image

Answer by GamingNewBie · Feb 17, 2013 at 03:09 PM

For better understanding of Instantiating Game Objects, follow link

docs.unity3d.com/Documentation/Manual/InstantiatingPrefabs.html

secondly for adding script components dynamically

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

hope it helps.

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Instantiate and initialize script 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Instantiating a random dropped consumable item from many cloned objects 1 Answer

Clones with individual scripts 1 Answer

Code only affects first clone? 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