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 spiralfire11 · May 21, 2015 at 12:07 AM · gameobjectgetcomponentstringsetactive

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

Comment
Add comment · Show 4
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 DiegoSLTS · May 21, 2015 at 12:22 AM 0
Share

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.

avatar image spiralfire11 · May 21, 2015 at 12:30 AM 0
Share

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?)

avatar image Mehul-Rughani · May 21, 2015 at 06:28 AM 0
Share

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;


avatar image spiralfire11 · May 21, 2015 at 08:05 PM 0
Share

Alright thanks, I think I found an answer thanks to your

string s=myLoader.GetComponent().characterToUse;

1 Reply

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

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 () {

 }

}

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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