// Use this for initialization //the order of everything in here is kind of fragile? void Start() { bookXPosition = 1.7f; bookYPosition = 4.1f; bookZPosition = -7.0f; shelves = GameObject.FindGameObjectsWithTag("shelf"); GameObject currentBookshelf = shelves[currentBookshelfNum]; string path = Application.dataPath + "/Text"; string[] filePaths = Directory.GetFiles(@path, "*.txt"); for (int i = 0; i <= collectionLoops; i++) { foreach (string file in filePaths) { //create the new book, assign it's details int startNamePosition = file.LastIndexOf('/'); //where does the name of the text file start? startNamePosition += 6; //deal with the fact that it likes to include "/Text\" string fileName = file.Substring(startNamePosition); GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); shelfBook currentBook = cube.AddComponent <shelfBook>() as shelfBook; currentBook.setName(fileName); //set position if (bookZPosition <= SHELF_WIDTH) //this book will fit on the shelf { currentBook.transform.parent = currentBookshelf.transform; currentBook.transform.localPosition = new Vector3(bookXPosition, bookYPosition, bookZPosition); bookZPosition += 7.0f; bookXPosition += 2.0f; currentBook.transform.localEulerAngles = new Vector3(0, 285, 0); //Debug.Log(currentBook + "placed at " + currentBook.transform.position); } else { if (currentShelfNum < 5) //there's another shelf on this bookshelf { bookZPosition = -7.0f; bookYPosition -= SHELF_SPACING; bookXPosition = 1.7f; currentShelfNum++; currentBook.transform.parent = currentBookshelf.transform; currentBook.transform.localPosition = new Vector3(bookXPosition, bookYPosition, bookZPosition); bookZPosition += 7.0f; bookXPosition += 2.0f; currentBook.transform.localEulerAngles = new Vector3(0, 285, 0); //Debug.Log(currentBook + "placed at " + currentBook.transform.position); } else //go to the next bookshelf { currentBookshelfNum++; currentBookshelf = shelves [currentBookshelfNum]; bookXPosition = 1.7f; bookYPosition = 4.1f; bookZPosition = -7.0f; currentShelfNum = 1; currentBook.transform.parent = currentBookshelf.transform; currentBook.transform.localPosition = new Vector3(bookXPosition, bookYPosition, bookZPosition); bookZPosition += 7.0f; bookXPosition += 2.0f; currentBook.transform.localEulerAngles = new Vector3(0, 285, 0); //Debug.Log(currentBook + "placed at " + currentBook.transform.position); } } //the good way to go to the next book, but currently broken. // bookZPosition += currentBook.getWidth(); // Debug.Log("increment by: "+ currentBook.getWidth()); // Debug.Log("New Z> "+ bookZPosition); //set remaining book details string[] nameParts = fileName.Split('-'); Debug.Log(nameParts.ToString()); try { currentBook.setAuthor(nameParts[0]); } catch (Exception e) { currentBook.setAuthor("ERROR"); } try { currentBook.setTitle(nameParts[1]); } catch (Exception e) { currentBook.setTitle("ERROR"); } } } }
// Update is called once per frame void Update() { //close book if (Input.GetKeyDown("x")) { Debug.Log("current array: " + openBooks[0] + ", " + openBooks[1] + ", " + openBooks[2] + ", " + openBooks[3] + ", " + openBooks[4]); Debug.Log("closing something?"); if (selectedBook > -1) { numOpenBooks--; Destroy(openBooks[selectedBook]); openBooks[selectedBook] = null; //(unclear if you need to do this excplicitly) tooManyBooks = false; //this will always be true if you've just closed a book //reshelve GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); shelfBook currentBook = cube.AddComponent <shelfBook>() as shelfBook; currentBook.setName(titles[selectedBook]); currentBook.transform.parent = putBookBackonthisShelves[selectedBook].transform; //! currentBook.transform.position = storedBookLocations[selectedBook]; string[] nameParts = titles[selectedBook].Split('-'); currentBook.setAuthor(nameParts[0]); currentBook.setTitle(nameParts[1]); currentBook.transform.localEulerAngles = new Vector3(0, 285, 0); selectedBook = -1; //you are no longer selecting a book, because you just closed it Debug.Log("updated array: " + openBooks[0] + ", " + openBooks[1] + ", " + openBooks[2] + ", " + openBooks[3] + ", " + openBooks[4]); } } //open book if (Input.GetKeyDown("space") && tooManyBooks == false || handsAreTouching == true && tooManyBooks == false) { Debug.Log("Opening a book now"); if (grabLeft.isHolding() || grabRight.isHolding()) //you'll need to sort this out more if which hand is grabbing matters //you're definitely not holding anything anymore { grabLeft.holding = false; grabRight.holding = false; if (heldObject.tag == "closedBook") { title = heldObject.name; Debug.Log("the title of this book is " + title); if (numOpenBooks < 4) { book = Instantiate(openBook); } int arrayPosition; if (numOpenBooks == 0) { openBooks[numOpenBooks] = book.gameObject; arrayPosition = 0; } else { arrayPosition = System.Array.IndexOf(openBooks, null); //this line might be the problem if (arrayPosition < 4 && arrayPosition >= 0) //seriously making sure this isn't broken { openBooks[arrayPosition] = book.gameObject; } } Debug.Log("placed in the array at " + arrayPosition); Debug.Log("number of open books: " + numOpenBooks); //Debug.Log("current array: " + openBooks[0] + ", " + openBooks[1] + ", " + openBooks[2] + ", " + openBooks[3] + ", " + openBooks[4]); //position opened book relative to the camera if (arrayPosition == 0) { Debug.Log("You're opening book 0"); book.transform.SetParent(Camera.main.transform); book.transform.localPosition = new Vector3(-115, -50, 150); book.transform.localEulerAngles = new Vector3(0, 0, 0); } else if (arrayPosition == 1) { Debug.Log("You're opening book 1"); book.transform.SetParent(Camera.main.transform); book.transform.localPosition = new Vector3(115, -50, 150); //these numbers need hard-coded jiggling book.transform.localEulerAngles = new Vector3(0, 0, 0); } else if (arrayPosition == 2) { Debug.Log("You're opening book 2"); book.transform.SetParent(Camera.main.transform); book.transform.localPosition = new Vector3(-115, 60, 150); //these numbers need hard-coded jiggling book.transform.localEulerAngles = new Vector3(0, 0, 0); } else if (arrayPosition == 3) { book.transform.SetParent(Camera.main.transform); book.transform.localPosition = new Vector3(115, 60, 150); //these numbers need hard-coded jiggling book.transform.localEulerAngles = new Vector3(0, 0, 0); } // else if(arrayPosition == 4){ // book.transform.SetParent(Camera.main.transform); // book.transform.localPosition = new Vector3(115, 0, 150);//these numbers need hard-coded jiggling // book.transform.localEulerAngles = new Vector3(0, 0, 0); // } // else if(arrayPosition == 5){ // book.transform.SetParent(Camera.main.transform); // book.transform.localPosition = new Vector3(-115, 0, 150);//these numbers need hard-coded jiggling // book.transform.localEulerAngles = new Vector3(0, 0, 0); // } // else if(arrayPosition == 6){ // book.transform.SetParent(Camera.main.transform); // book.transform.localPosition = new Vector3(50, -50, 150);//these numbers need hard-coded jiggling // book.transform.localEulerAngles = new Vector3(0, 0, 0); // } // else if(arrayPosition == 7){ // book.transform.SetParent(Camera.main.transform); // book.transform.localPosition = new Vector3(-50, -50, 150);//these numbers need hard-coded jiggling // book.transform.localEulerAngles = new Vector3(0, 0, 0); // } // else if(arrayPosition == 8){ // book.transform.SetParent(Camera.main.transform); // book.transform.localPosition = new Vector3(50, 60, 150);//these numbers need hard-coded jiggling // book.transform.localEulerAngles = new Vector3(0, 0, 0); // } // else if(arrayPosition == 9){ // book.transform.SetParent(Camera.main.transform); // book.transform.localPosition = new Vector3(-50, 60, 150);//these numbers need hard-coded jiggling // book.transform.localEulerAngles = new Vector3(0, 0, 0); // tooManyBooks = true; // } if (numOpenBooks < 4) { titles[arrayPosition] = title; Destroy(heldObject); //does this set things to null or do you now have a weird broken ref? numOpenBooks++; //clear out variables (this is garbage that returns a null error but its fine) heldObject = null; held = null; title = "no book selected"; Debug.Log("updated array: " + openBooks[0] + ", " + openBooks[1] + ", " + openBooks[2] + ", " + openBooks[3] + ", " + openBooks[4]); } else { Debug.Log("There are too many books open! Close a book in order to open this one."); } } } } }
// Update is called once per frame void Update() { //close book if (Input.GetKeyDown("x")) { numOpenBooks--; Destroy(openBooks[numOpenBooks]); tooManyBooks = false; //this will always be true if you've just closed a book //reshelve GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); shelfBook currentBook = cube.AddComponent <shelfBook>() as shelfBook; currentBook.setName(title); currentBook.transform.parent = putBookBackonthisShelves[numOpenBooks].transform; //! currentBook.transform.position = storedBookLocations[numOpenBooks]; string[] nameParts = title.Split('-'); currentBook.setAuthor(nameParts[0]); currentBook.setTitle(nameParts[1]); currentBook.transform.localEulerAngles = new Vector3(0, 285, 0); } //open book if (Input.GetKeyDown("space") && tooManyBooks == false || handsAreTouching == true && tooManyBooks == false) { Debug.Log("Opening a book now"); if (grabLeft.isHolding() || grabRight.isHolding()) //you'll need to sort this out more when which hand is grabbing matters { if (heldObject.tag == "closedBook") { title = heldObject.name; Debug.Log("the title of this book is " + title); book = Instantiate(openBook); openBooks[numOpenBooks] = book.gameObject; //unity doesn't like this??? <------------------this is the problem right now Debug.Log("number of open books: " + numOpenBooks); //position opened book relative to the camera if (numOpenBooks == 0) { Debug.Log("You're opening book 0"); book.transform.SetParent(Camera.main.transform); book.transform.localPosition = new Vector3(-115, -50, 150); book.transform.localEulerAngles = new Vector3(0, 0, 0); } else if (numOpenBooks == 1) { Debug.Log("You're opening book 1"); book.transform.SetParent(Camera.main.transform); book.transform.localPosition = new Vector3(115, -50, 150); //these numbers need hard-coded jiggling book.transform.localEulerAngles = new Vector3(0, 0, 0); } else if (numOpenBooks == 2) { Debug.Log("You're opening book 2"); book.transform.SetParent(Camera.main.transform); book.transform.localPosition = new Vector3(-115, 60, 150); //these numbers need hard-coded jiggling book.transform.localEulerAngles = new Vector3(0, 0, 0); } else if (numOpenBooks == 3) { book.transform.SetParent(Camera.main.transform); book.transform.localPosition = new Vector3(115, 60, 150); //these numbers need hard-coded jiggling book.transform.localEulerAngles = new Vector3(0, 0, 0); } else if (numOpenBooks == 4) { book.transform.SetParent(Camera.main.transform); book.transform.localPosition = new Vector3(115, 0, 150); //these numbers need hard-coded jiggling book.transform.localEulerAngles = new Vector3(0, 0, 0); } else if (numOpenBooks == 5) { book.transform.SetParent(Camera.main.transform); book.transform.localPosition = new Vector3(-115, 0, 150); //these numbers need hard-coded jiggling book.transform.localEulerAngles = new Vector3(0, 0, 0); } else if (numOpenBooks == 6) { book.transform.SetParent(Camera.main.transform); book.transform.localPosition = new Vector3(50, -50, 150); //these numbers need hard-coded jiggling book.transform.localEulerAngles = new Vector3(0, 0, 0); } else if (numOpenBooks == 7) { book.transform.SetParent(Camera.main.transform); book.transform.localPosition = new Vector3(-50, -50, 150); //these numbers need hard-coded jiggling book.transform.localEulerAngles = new Vector3(0, 0, 0); } else if (numOpenBooks == 8) { book.transform.SetParent(Camera.main.transform); book.transform.localPosition = new Vector3(50, 60, 150); //these numbers need hard-coded jiggling book.transform.localEulerAngles = new Vector3(0, 0, 0); } else if (numOpenBooks == 9) { book.transform.SetParent(Camera.main.transform); book.transform.localPosition = new Vector3(-50, 60, 150); //these numbers need hard-coded jiggling book.transform.localEulerAngles = new Vector3(0, 0, 0); tooManyBooks = true; } Destroy(heldObject); //does this set things to null or do you now have a weird broken ref? numOpenBooks++; //clear out variables (this is garbage that returns a null error but its fine) heldObject = null; held = null; title = "no book selected"; } } } }