- Home /
Add Fresh Script to Game Object
Hi,
I have a OnGUI Character Creation Menu where I can select 2 different characters. Based on my selection I want to add a "fresh" script onto an existing game object.
using UnityEngine; using System.Collections;
public class CharacterSelection : MonoBehaviour { public static int userCharacter = 0;
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;
Debug.Log(userCharacter);
gameObject.AddComponent(typeof(CharacterA));
Application.LoadLevel("CharacterCreationScreen"); } if (GUI.Button (new Rect (100,160,200,20), "CharacterB")) { userCharacter = 2; Debug.Log(userCharacter); gameObject.AddComponent(typeof(CharacterB)); Application.LoadLevel("CharacterCreationScreen"); } }
}
What I want is to choose a character and have the game know that I want this script loaded onto a game object that I have in a scene that I eventually get to..........
whoa okay I think I see how this works, and its insane.. For my main character I am "double" adding the component, so my movements are all crazy-like. When I do add the movements it seems that it would be global for all player classes..wow...how do I manage this? ;x
Answer by Ashkan_gc · Jun 02, 2010 at 06:41 AM
your descriptiont is not clear but i try to show you ways for connection gameObjects together. 1 you can check the static variable of this class in OnLevelLoaded method of your next level's object and load the script based on it's value. 2 you can have an object that don't destroy when you change the scene (use DontDestroyOnLoad in Awake function) and use it's OnLevelLoaded method to do what you want by this gameObject. for adding a script to a gameObject you should use AddComponent
Right, I forgot to include the fact that I can't use just AddComponent by itself. I need to include gameObject.AddComponent(typeof Type)). Your DontDestoryOnLoad and Awake suggestions will definitely help...Thanks
Your answer
Follow this Question
Related Questions
Adding variable not work in an extended script 0 Answers
Adding animation transition between idle and walk 1 Answer
Parallel event in scripts?? One affects the other?? 0 Answers
error saying first person controller prefab is missing Camera wont let me mouselook in game 1 Answer
adding script to a clone 2 Answers