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
1
Question by James Ser · Jun 01, 2010 at 02:02 AM · stringbooleanconvert

Converting string to boolean

Hi guys,

I have a few doors and few keys to open specific doors. what I am trying to do is to create a function so that i can reuse them.

The problem that i encounter is converting a series of string to boolean. Can't use parseBoolean(); Don't even know that it is the correct way to do it or is there a better way? Please assist and thanks in advance!

var doorNum: int; static var accessGranted_1: boolean; var isOpen_1: boolean = false;

checkTileOpenDoor_spaceship(1);

function checkTileOpenDoor_spaceship(doorNum){

 var doorIndex: int = doorNum;       
 var currentTileTypeIndex: String = "TileOpenDoor_" + doorIndex;
 var isOpenIndex: boolean = parseBoolean("isOpen_" + doorIndex);
 var accessGrantedIndex: boolean = parseBoolean("accessGranted_" + doorIndex);
 var TileEndIndex: String = "TileEnd_" + doorIndex;

 if (currentTileType == currentTileTypeIndex && !isOpenIndex && accessGranted){
     //open door

     GameObject.Find(TileEndIndex).animation.wrapMode = WrapMode.Once;
     GameObject.Find(TileEndIndex).animation.Play("open");
     isOpenIndex = true;

 } else if (currentTileType != currentTileTypeIndex && isOpenIndex){
     //close door

     GameObject.Find(TileEndIndex).animation.wrapMode = WrapMode.Once;
     GameObject.Find(TileEndIndex).animation.Play("close");
     isOpenIndex = false;

 }

}

Rdgs, James Ser

Comment
Add comment · Show 1
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 alexnode · Aug 13, 2010 at 03:27 PM 0
Share

i was thinking about the same thing, it can be really handy really.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Ashkan_gc · Jun 01, 2010 at 02:10 AM

you should use bool.Parse to convert a string to it's logical boolean representation.

Comment
Add comment · Show 5 · 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 Cyclops · Jun 01, 2010 at 02:34 AM 0
Share

This may be a better link for it: http://msdn.microsoft.com/en-us/library/system.boolean.parse.aspx

avatar image James Ser · Jun 01, 2010 at 02:51 AM 0
Share

Thanks for your suggestion. But it doesn't work. It says unknown identifier: 'bool'

avatar image Ashkan_gc · Jun 01, 2010 at 09:01 AM 0
Share

in C# the class name is bool. in js you might need to use another name like boolean. i don't know. i can create a C# class that exposes this method for you. tell me if you need and i'll edit the answer.

avatar image alexnode · Aug 13, 2010 at 03:37 PM 0
Share

bool.Parse passes user input of true or false to a bool. What jamesser asks is to have a series of bool , like doorIsOpenFloor01 = false, doorIsOpenFloor02 =false, and a var currentfloor: int ...etc and in your script to call them as "if so and so doorIsOpen0(currentFloor) = false

avatar image NewfieJoe · May 04, 2011 at 11:25 PM 0
Share

In UnityScript (javascript) it's boolean.Parse() like Ashkan thought for anyone reading this later.

avatar image
1

Answer by NewfieJoe · May 04, 2011 at 11:38 PM

You can create a hashtable and store the values in it then read it from there if you absolutely need to reference them this way. Easier would be to use boolean arrays.

public var doorNum: int; static var accessGranted: boolean[]; public var isOpen: boolean[];

function checkTileOpenDoor_spaceship( door: int ){

var currentTileTypeIndex: String = "TileOpenDoor_" + door; var TileEndIndex: String = "TileEnd_" + door;

if (currentTileType == currentTileTypeIndex && !isOpen[ door ] && accessGranted [ door ]){ //open door

 GameObject.Find(TileEndIndex).animation.wrapMode = WrapMode.Once;
 GameObject.Find(TileEndIndex).animation.Play("open");
 isOpen[ door ] = true;

} else if (currentTileType != currentTileTypeIndex && isOpen[ door ]){ //close door

 GameObject.Find(TileEndIndex).animation.wrapMode = WrapMode.Once;
 GameObject.Find(TileEndIndex).animation.Play("close");
 isOpen[ door ] = false;

} }

Comment
Add comment · Show 2 · 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 Owen-Reynolds · May 05, 2011 at 02:14 AM 0
Share

The key is that if you have 8 doors, you need to declare 8 booleans for isOpen and 8 bools for accessGranted. The only good way to do that is with arrays.

avatar image NewfieJoe · May 05, 2011 at 05:18 AM 0
Share

Yeah, that's why I thought they'd be best. You could have hundreds and it will be fine.

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

No one has followed this question yet.

Related Questions

How to convert a string to int array in Unity C# 1 Answer

Converting 1s and 0s to float 1 Answer

Level Selection and Strange Error 0 Answers

Redirecting input with event.current.character? 1 Answer

How to call a variable using a String for the variable name? 1 Answer


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