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 lampshade · Jun 02, 2010 at 12:04 AM · addcomponent

Loading Scripts onto GO before in-game scene

Hi,

What I'd like to do, is to be able to choose a type of character, CharacterA or CharacterB, both with different functionality and load their script onto a my Player GameObject. If I choose CharacterA I should load CharacterA's script, and should be able to use the functionality that I have programmed for that script. If I choose CharacterB I should be able to load that different functionality, so on and so forth.

From What I understand I can use the: FoobarScript fbs = gameObject.AddComponent(); reference in the documentation. I am not getting any errors, just what I want to happen, isnt. Here is a snippet of code to show what is relevent to my problem:

// GUI Script for the "Select Your Character" Scene

void OnGUI() { GUI.Box (new Rect (0, 0, Screen.width, Screen.height), "Select Your Character");

 if (GUI.Button (new Rect (100,100,200,20), "CharacterA")) 
 {
     userCharacter = 1; // back up plan? ;x
     CharacterA CharA = gameObject.AddComponent<CharacterA>();
     Application.LoadLevel("CharacterCreationScreen"); // Proceed into game
 }
 if (GUI.Button (new Rect (100,160,200,20), "CharacterB")) 
 {
     userCharacter = 2;
     CharacterB CharB = gameObject.AddComponent<CharacterB>();
     Application.LoadLevel("CharacterCreationScreen"); // Proceed into game
 }

}

But I dont see the functionality, indicating that the script did not load..while in game. I include a standalone statement in the script, namely, a Debug.Log("hi") and I do see that appear directly after I select CharacterB. But I do not experience the functionality in the in-game scene.

I also disabled any scripts for the Player (GameObject) before running each scene. As always any help is very much appreciated.

Comment
Add comment · 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 lampshade · Jun 02, 2010 at 12:15 AM 0
Share

$$anonymous$$y Character has several prefabs, set $$anonymous$$ove and Rotate Speeds, and Game Components. What I think, is that I need to write down everything about the player, and Add each of these components seperately.

1 Reply

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

Answer by qJake · Jun 02, 2010 at 12:34 AM

What you should probably do is have a "Settings" game object and script that are "global", i.e. when your game starts up, call DontDestroyOnLoad(mySettingsObject); This will preserve this object even if you load a new level. So what you can do is select your character, save it into the Settings object, and then when your next level/scene loads, read the value and insert the proper component.

Also, I'm not sure you're calling AddComponent correctly... you want to call it on the game object of your character, and currently, you're calling it on the game object that contains the GUI code. I'm not sure if these are the same (They probably aren't), but you need to call AddComponent on your character's game object in order for this method to work. Something like this:

// This script goes on your character game object.

void Start() { // Get Settings Object somehow, either a reference or via "Find" // Settings settings = ...; <-- fill this in

  // These values are made-up, but you can see what I'm getting at.
  if(settings.character == 1)
  {
       AddComponent(typeof(CharacterA));
  }
  else if(settings.character == 2)
  {
       AddComponent(typeof(CharacterB));
  }

}

Comment
Add comment · Show 8 · 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 lampshade · Jun 02, 2010 at 01:00 AM 0
Share

As you might already know, I am program$$anonymous$$g in C#, and the syntax for AddComponent differs. I tried using the syntax for the Unity docs, and the if else block you set up, but still nothing ;/

avatar image lampshade · Jun 02, 2010 at 01:24 AM 0
Share

Its unfortunate that gameObject.AddComponent(ScriptName) or AddComponent(ScriptName) or event with "ScriptName" does not work..

avatar image qJake · Jun 02, 2010 at 02:17 AM 0
Share

"gameObject.AddComponent" and "AddComponent" are exactly the same thing. AddComponent takes a Type object, or a string representation of a type object. This code is in C# (hence the void Start()), and it should work fine, so long as you have $$anonymous$$onoBehaviour scripts called "CharacterA/B", you stuck this script on your Character game object in your second level, and you followed all the instructions I wrote.

avatar image lampshade · Jun 02, 2010 at 03:00 AM 0
Share

I see a difference only when I the script already enabled for CharacterA/B. The in-game GUI is overwritten and it lags terribly. I tried to destory the Component before I loaded it again and that didnt work either. I've followed everything that you have written.

avatar image lampshade · Jun 02, 2010 at 03:01 AM 0
Share

Also for some reason, AddComponent(typeof(type)) does not work. I need to use gameObject.AddComponent(typeof(type)), since they are that should be okay. But it is still not working.

Show more comments

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

GetComponent and AddComponent with variables for Collider type 2 Answers

A Silly simple question about scope 1 Answer

Destroying Object when Adding Component 2 Answers

How to attach a script to an instantiated object? 1 Answer

Creating a Script at runtime, and adding it to an object... 1 Answer


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