- Home /
Accessing a Certain Script
Hello,
I am trying to create a "Back" button for my in-game login menu. The way it works so far is this: Menu > "Login" or "Update"
If you clicked "Login", it will do "Instantiate(LoginPrefab)"; if you clicked "Update", it will do "Instantiate(UpdatePrefab)". Simple.
I need a back button for both instantiated prefabs that will destroy the current instantiated prefab and then send the message "ActivateMenu, true" to a script called "crMenuScript.js" that is in the "Menu" prefab, the one where my menu runs from. ActivateMenu lets me use the menu again.
I've done multiple tests, but I can only destroy the instantiated prefab. I always get a "NullReferenceException" when trying to access "crMenuScript" from another prefab.
Forgive me for the newbish post, but I have tried everything I could do and find over the past couple weeks. All of Unity's scripting reference pages could not help me (perhaps I was using the scripts the wrong way).
Thank you very much,
TheCyberMan
P.S. I would prefer this to be in Javascript, but I just need guidance on getting this to work. I could translate the language to Javascript later if need be.
Why are you trying to delete the prefab? You shouldn't be able to do that in any case. I think you aren't doing this in a very good way- what do the objects with these various scripts on them do other than draw the menus? You could put all of these on a single object, and then enable/disable them when you need to.
The menu is a prefab itself. When I click the "Login" button, it instantiates a clone of the login system's prefab. I can't put the other prefabs inside of the menu prefab. I want to delete the prefab so that when I click "Back" it destroys the clone.
A prefab never 'executes' at runtime. I still don't understand why you can't just enable and disable the menus when you need to. I understand how the system works now, I just don't think it's a good method. You haven't given me a compelling reason why it has to work the way you are trying to do it. You can have many components on a single gameObject, and you can create and destroy those components at runtime easily.
"Login" and "Update" have two separate prefabs that activate when their respective button is clicked. What do you recommend I do then? This is very new for me.
Thank you.
Answer by Justin Warner · Dec 09, 2011 at 03:51 AM
Rather than do prefabs with different GUI stuff...
do something simliar:
var showMain = true;
var showLevels = false;
var showOptions = false;
var showExitSplash = false;
function OnGUI()
{
if(showMain)
//Show main GUI crap.
//If (Button here for levels, if clicked do this down here.)
//showMain = false; showLevels = true;
if(showLevels)
//Show level selection
//If (Button here for back to main, if clicked do this down here.)
//showMain = true; showLevels = false;
if(showOptions)
//Show options stuff
//If (Button here for back to main, if clicked do this down here.)
//showMain = true; showOptions = false;
if(showExitSplash)
//Do splash screen then exit
}
That's how I do mine... Think of OnGUI as Update() so that it runs every frame, so if something is false, it won't do under that, if it's true it will. So then you can disable parts of your GUI, being kind of like layers.
=)
By the way, if you need prefabs, then you can just Destroy(prefab1) before you change the boolean values, then it'll be good =).
I realize this is the better way to go, so I'm switching my menu system. Thank you for your time!
Your answer
Follow this Question
Related Questions
What should a script contain? 1 Answer
How can I tell what prefabs a child object (script) is in? 2 Answers
Tagging object while instating not working. 3 Answers
When my prefabs are instantiated they don't have their script attached 1 Answer
script won't affect prefabs that were placed in the scene multiple times 1 Answer