- Home /
Ok so im trying to get a string's name from another script to enable (SetActive) a certain gameObject in the scene (the player); however I keep getting errors and i have no idea what to do?? any help???
MainMenu Script :
using UnityEngine; using System.Collections;
public class MainMenu : MonoBehaviour {
// Use this for initialization
public string characterToUse;
void Awake() {
DontDestroyOnLoad(gameObject);
}
void Start () {
Destroy(GameObject.FindGameObjectWithTag("Player"));
characterToUse = "None";
}
// Update is called once per frame
void Update () {
//Getfromthe MenuGUI : if CharacterToUse = blablabla, this script loads certain scene and doesnt destroy on next scene;
//Once the other Scene is loaded, make "CharacterToUse" = AlreadyUsing, as such it won't activate another character on load, thanks to the DontDestroyOnLoad
}
}
SceneLoader script(A.K.A the player name getter) :
using UnityEngine; using System.Collections;
public class SceneCharLoad : MonoBehaviour {
// Use this for initialization
void Start () {
GameObject myLoader = GameObject.Find("TheLoader");
myLoader.GetComponent<MainMenu>();
myLoader.characterToUse.SetActive(true);
}
// Update is called once per frame
void Update () {
}
}
And Yes, i have made a prefab to use the MainMenu script on e.e
With those scripts is really hard to help you. Copy the error messages and explain with more details how's your scene setup.
I see you're setting the $$anonymous$$ain$$anonymous$$enu script to not destroy on load level but it's not clear way, usually the DontDestroyOnLoad is used to create "singleton" gameobjects, but you're not doing that, you might end with multiple $$anonymous$$ain$$anonymous$$enu objects in the same scene.
I also see you're looking for a game object with the tag "Player" and destroying it immediatelly, and again, it's not clear way, it kind of contradicts your question: you wrote you want to SetActive the player, you can't do that if you destroy it.
And then there's the comment inside the Update, where it looks like you just expect someone to write the code for you following some instructions. That's not how things work here, write something and someone will tell you how to fix it if it's not working correctly.
Well, the comments and the DontDestroyOnLoad are not really giving me problems, its just notes I'm using for later since the scripts are Drafts I'm going to use later on. Thanks for the Destroy(GameObject.FindGameObjectWithTag("Player")), i hadn't noticed that contradiction because i was focusing on something else. Anyways, the error I receive is the following :
error CS1061: Type UnityEngine.GameObject' does not contain a definition for
characterToUse' and no extension method characterToUse' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
characterToUse is not an gameObject so you cant SetActive that thing.. I think you are looking for this..
GameObject myLoader = GameObject.Find("TheLoader");
string s=myLoader.GetComponent<$$anonymous$$ain$$anonymous$$enu>().characterToUse;
it will return characterToUse value(string) if any
if you want to active or deactive loader gameobject then you can directly use..
myLoader.SetActive(true/false);
or if you want main$$anonymous$$enu script to enable or disable then you have to use..
myLoader.GetComponent<$$anonymous$$ain$$anonymous$$enu>().enabled=true/false;
Alright thanks, I think I found an answer thanks to your
string s=myLoader.GetComponent().characterToUse;
Answer by spiralfire11 · May 21, 2015 at 08:05 PM
idk if it will work, but for now this is the answer :
using UnityEngine;
using System.Collections;
public class SceneCharLoad : MonoBehaviour {
// Use this for initialization
void Start () {
GameObject myLoader = GameObject.Find("TheLoader");
string getCharacter = myLoader.GetComponent<MainMenu>().characterToUse;
GameObject playerToUse = GameObject.Find(getCharacter);
playerToUse.SetActive(true);
}
// Update is called once per frame
void Update () {
}
}
Your answer
Follow this Question
Related Questions
GetComponent() - Is it possible to pass a string variable as name of the script? 3 Answers
Getting errors when referencing a variable. 1 Answer
GameObjects that I deactivate are reactivated immediately 0 Answers
c# Set Active error, conflicting with other code.. 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers