Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 spdoriano · Jan 02, 2019 at 09:54 PM · 2d game2d-platformer

Trying to destory a character on a different scene with a UI button

Hello there, rookie here, I am having issues with my current code. I am currenly making a character select screen and want to have the other characters already loaded into the game on the main game screen.

On the start menu there will be two buttons that will allow you to be able to pick between one of the two charatcers and upon interaction with one of the buttons, will destory the character that wasnt chosen on the next scene and the game will be played.

This is what i have so far and thank you for the hlep in advance :) !

3ed0409933e37bf44e95400b3123b859.png (13.3 kB)
Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by JxWolfe · Jan 02, 2019 at 10:18 PM

Basically if you need to set a variable telling which character is chosen, then make that script variable static. Then when you load your scene check that static variable to destroy the other player.

Ok, looking at your code there is one BIG issue... Your making a class INSIDE another class. Two options, either do this,

 public static class GlobalVariables
 {
   public bool characterChoice;
 }
 
 public class Elane_Button : MonoBehaviour {
 
   void Start()
   {
     if(GlobalVariables.characterChoice == true)
     {
       //do something
     }
   }
 }

or

 public class Elane_Button : MonoBehaviour {
 
   public static bool characterChoice;
 
   void Start()
   {
       if(characterChoice == true) //maybe Elane_Button.characterChoice if that doesn't work
       {
           //do something
       }
    }
 }

Either of those approaches SHOULD work... I may have messed up -- Hope it helps, again please ask if you need more assistance.

Comment
Add comment · Show 7 · 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 JxWolfe · Jan 03, 2019 at 07:09 PM 0
Share

ok, so here's an idea that I had... If you ever plan of expanding your characterChoices for you game, you may wish to change characterChoice into a enum... that way you could have thousands of characters without changing your variable... if you like that idea, I will be happy to supply the code.

avatar image spdoriano · Jan 03, 2019 at 07:37 PM 0
Share

right I scrapped my code and have re-writen it and its works till the "Destroy(Player)". From what I know and from what unity is telling me, I can't use "Player" because it is a "type that cannot be given context" im not sure what I should put in the brackets as I know if I put "gameObject" I will then have to attach the script to the object in that same scene, which it isnt in. Or am I also right in saying that i can attatch the script to the player even though it is in a different scene and still call it "gameObject"?

avatar image JxWolfe · Jan 03, 2019 at 07:59 PM 0
Share

ok, here's what I would do... On your character's script -- add this command in your Start()

 public bool defaultCharacter; 
 //basicly if characterChoice == true, then it's selecting our character if defaultCharacter == true;
 
 void Start()
 {
 if(Elane_Button.characterChoice != defaultCharacter)
 {
 //it says that the characterChoice isn't our character
 Destroy(gameObject);
 }
 }

So that needs to go in the CHARACTER'S SCRIPT... because we can reference a static variable without any additional steps.

avatar image spdoriano JxWolfe · Jan 03, 2019 at 08:15 PM 0
Share

ok yeah that is working now, the only issue i have now is that when I attach the "Elane_button" script, I need to then link that to the button which i have done, but I am unsure about is what on event click I should be using. The image below is what I mean, had to get another user to upload it.

avatar image KeroCarlito spdoriano · Jan 03, 2019 at 08:30 PM 0
Share

alt text

thingy-thongy.png (18.7 kB)
Show more comments
avatar image
1

Answer by Shaolin-Dave · Jan 03, 2019 at 10:35 PM

You've provided code that shows it's checking the value of your static bool, but not anything that shows the bool ever being populated. You should share your button code. Also, add debug logs to make sure that the bool is both being set and read properly. I have a feeling it's checking for true/false values but actually finding null.

Beyond that, this isn't a very good way to select characters: My suggestion is to have your button populate and enum or select a prefab GameObject from an array of available characters. Create a "spawner" GameObject that will spawn the one character you want to play as, not a script to destroy the ones you don't want.

     void Start()
     {
         Debug.Log(“characterChoice = “ + characterChoice);
     
         if (!characterChoice) {
             // Instantiate Player2 GameObject Here
         } else {
             // Instantiate Player1 (default) GameObject Here
         }
     }

Comment
Add comment · Show 1 · 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 spdoriano · Jan 04, 2019 at 11:51 AM 0
Share

I did try to make a character select screen before using a "spawner" but came t the issue that one of my scripts would not link itself to the characater on that scene as it did not exist on that scene, even using a prefab, it still never worked, hence why i am trying to do it as above.

avatar image
0

Answer by spdoriano · Jan 03, 2019 at 05:59 PM

@JxWolfe Hi there, i have tried to make the varaible a static bool variable but unity will not detect it for some reason, i feel like i have nit set it right? (see pic in post)

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

Answer by kqmdjc8 · Jan 03, 2019 at 06:23 PM

Try using "==" instead of "="

Comment
Add comment · Show 1 · 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 spdoriano · Jan 03, 2019 at 06:35 PM 0
Share

ive added that and it says that the variable does not exist in that context, this is leading me to belive i need to use a different variable instaed of "characterChoice"? @kqmdjc8

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

120 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 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 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 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 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 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

OnTriggerEnter2D(Collider2D other) 2 Answers

Add audio. 1 Answer

2d platformer script problems 0 Answers

"Create New Palette" doesn't actually create a palette at all 1 Answer

Cinemachine camera shake on button press 0 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