Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to not "destroy" an object but "save it for later"

Discussion in 'Scripting' started by Hotpots, Oct 16, 2015.

  1. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Hey guys,
    So I'm having problems with picking up and dropping items, when I pick up an item I destroy the item through the script, thus when I want to drop the item and spawn it back in front of the player I get an error.

    What I want it to do is:
    1. The player drops the item:
    2. The player sees the object they drop (in this case a grey cube).

    My code so far:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ObjectPickup : MonoBehaviour
    6. {
    7.     public Image bookimage;
    8.     public Image mapimage;
    9.  
    10.     float bookweight = 1.2f;
    11.     float mapweight = 0.5f;
    12.     Color visible = new Color(0f, 0f, 0f, 100f);
    13.     Color invisible = new Color(0f, 0f, 0f, 0f);
    14.     bool isholding;
    15.  
    16.     GameObject book;
    17.     GameObject map;
    18.  
    19.     void Update()
    20.     {
    21.         if (Input.GetKey("q") && isholding == true)
    22.         {
    23.             DropItem();
    24.         }
    25.     }
    26.  
    27.     void Start()
    28.     {
    29.         book = GameObject.FindGameObjectWithTag("book");
    30.         map = GameObject.FindGameObjectWithTag("map");
    31.     }
    32.  
    33.     void OnTriggerStay(Collider other)
    34.     {
    35.         // If the entering collider is a book...
    36.         if (other.gameObject == book && Input.GetKey("e"))
    37.         {
    38.             ScoreManager.currentweight += bookweight;
    39.             Destroy(other.gameObject);
    40.             bookimage.color = visible;
    41.             isholding = true;
    42.         }
    43.         // If the entering collider is a map...
    44.         else if (other.gameObject == map && Input.GetKey("e"))
    45.         {
    46.             ScoreManager.currentweight += mapweight;
    47.             Destroy(other.gameObject);
    48.             mapimage.color = visible;
    49.             isholding = true;
    50.         }
    51.         else
    52.         {
    53.         }
    54.     }
    55.  
    56.     void DropItem()
    57.     {
    58.         isholding = false;
    59.         Debug.Log("False");
    60.         bookimage.color = invisible;
    61.         Instantiate(book);
    62.     }
    63. }
    The piece(s) of code creating the error:
    Code (CSharp):
    1. Destroy(other.gameObject);

    and then later...
    Code (CSharp):
    1. Instantiate(book);


    So what's another way of doing this without officially destroying the item?

    Thanks guys! :D
     
  2. GoofBall101

    GoofBall101

    Joined:
    Jul 25, 2015
    Posts:
    57
    I would save it for later by just translating it to a location where nobody could see it.Then when he drops it translate it to an "hand" which is what I think a person would have in your game.
     
    Last edited: Oct 16, 2015
    Kiwasi likes this.
  3. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    Create Prefabs for your item GameObjects and load them from Resources when you instantiate them.

    If you genuinely just want to cycle the same object from a held to a dropped position over and over (i.e. pick up, put down, pick up, put down) you can just toggle its Renderer component (enabled/disabled).
     
  4. sdviosx

    sdviosx

    Joined:
    Jan 4, 2015
    Posts:
    22
    Pooling is the way to go. Instead of destroying the game object, set its active state to false.
    Code (CSharp):
    1. void OnTriggerStay(Collider other)
    2.     {
    3.         if (other.gameObject == book && Input.GetKey("e"))
    4.         {
    5.             ...
    6.            other.gameObject.SetActive(false);
    7.         }
    Make sure to get a reference of the object you collided with so that on your drop method you could enable(myBookRef.SetActive(true)) it back again and set its position accordingly.
     
    Kiwasi, GeekStories and angrypenguin like this.
  5. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Thanks for all your comments and help guys. How would I set the position of the object say to be in relation to the player, but just 1 unit in front of them, after dropping the item.
     
  6. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    To set the objects position in relation to the player but 1 unit in front of them:

    Code (CSharp):
    1.  
    2. ///Say the player faces x coord..
    3. //This sets a Vector3 with the coords of the player, but on one axis 1 unit in front
    4. Vector3 pos = new Vector3(playerPosition.x + 1, playerposition.y, playerPosition.x);
    5. //Instantiate the item in front of the player
    6. Instantiate(item, pos, Quaternion.identity);
    7.  
    Just a quick guess, hope it helps! :)
     
  7. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Hey Geek!
    Thanks for the help, I can't quite get this to work...

    What I have so far...
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ObjectPickup : MonoBehaviour
    6. {
    7.     public Image bookimage;
    8.     public Image mapimage;
    9.  
    10.     float bookweight = 1.2f;
    11.     float mapweight = 0.5f;
    12.     Color visible = new Color(0f, 0f, 0f, 100f);
    13.     Color invisible = new Color(0f, 0f, 0f, 0f);
    14.     bool isholding;
    15.  
    16.     GameObject book;
    17.     GameObject map;
    18.     GameObject player;
    19.  
    20.     void Awake()
    21.     {
    22.         book = GameObject.FindGameObjectWithTag("book");
    23.         map = GameObject.FindGameObjectWithTag("map");
    24.         player = GameObject.FindGameObjectWithTag("Player");
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         Vector3 position = player.gameObject.transform.position;
    30.         if (Input.GetKey("q") && isholding == true)
    31.         {
    32.             DropItem();
    33.         }
    34.     }
    35.  
    36.     void OnTriggerStay(Collider other)
    37.     {
    38.         // If the entering collider is a book...
    39.         if (other.gameObject == book && Input.GetKey("e"))
    40.         {
    41.             ScoreManager.currentweight += bookweight;
    42.             other.gameObject.SetActive(false);
    43.             bookimage.color = visible;
    44.             isholding = true;
    45.         }
    46.         // If the entering collider is a map...
    47.         else if (other.gameObject == map && Input.GetKey("e"))
    48.         {
    49.             ScoreManager.currentweight += mapweight;
    50.             Destroy(other.gameObject);
    51.             mapimage.color = visible;
    52.             isholding = true;
    53.         }
    54.         else
    55.         {
    56.         }
    57.     }
    58.  
    59.     void DropItem()
    60.     {
    61.         isholding = false;
    62.         bookimage.color = invisible;
    63.         book.gameObject.SetActive(true);
    64.         Instantiate(book, position, Quaternion.identity);
    65.     }
    66. }
    I assume finding the players position in the update function is the correct thing to do as it's constantly changing.

    However how would I then instantiate the object at the player's position?

    My code works almost perfectly if I remove the instantiate line from the DropItem() function. My object reappears however obviously at it's original position and not JUST in front of the player.

    I appreciate the help once again! :)
     
  8. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    Setting the position for the book to spawn in front of the player will only need to be done once.

    So my guess would be something like this:

    Setting the position for the book to spawn in front of the player will only need to be done once.

    So my guess is something like this:

    Code (CSharp):
    1.  
    2. void DropItem()
    3. {
    4. isholding = false;
    5. bookimage.color = invisivle;
    6. book.gameObject.SeActive(true);
    7.  
    8. //Grab the players position in a temp Vector3 var (player facing x)
    9. Vector3 position = new Vector3(playerPosition.x + 1, playerPosition.y, PlayerPosition.z);
    10. //Create the item
    11. Instantiate(book, position, Quaternion.identity);
    12. }
    13.  
    This should create the book item 1 (Unity) unit in front of the player
     
  9. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    This may seem like a silly question but when I try this I get this error, what EXACTLY is playerPosition? :D:
     
  10. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    Haha, Alright the playerPosition, that would be
    Code (CSharp):
    1.  
    2. GameObject player; //line 18
    3. player = GameObject.FindGameObjectWithTag("Player"); //line 24
    4. Vector3 position = player.gameObject.transform.position; //line 29
    5.  
    Thats playerPosition :)
     
  11. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Hey Geek,
    Awesome I understand what your saying, I've adjusted the code, however I'm still getting an error:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ObjectPickup : MonoBehaviour
    6. {
    7.     public Image bookimage;
    8.     public Image mapimage;
    9.  
    10.     float bookweight = 1.2f;
    11.     float mapweight = 0.5f;
    12.     Color visible = new Color(0f, 0f, 0f, 100f);
    13.     Color invisible = new Color(0f, 0f, 0f, 0f);
    14.     bool isholding;
    15.  
    16.     GameObject book;
    17.     GameObject map;
    18.     GameObject player;
    19.  
    20.     void Awake()
    21.     {
    22.         book = GameObject.FindGameObjectWithTag("book");
    23.         map = GameObject.FindGameObjectWithTag("map");
    24.         player = GameObject.FindGameObjectWithTag("Player");
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         Vector3 playerPosition = player.gameObject.transform.position;
    30.         if (Input.GetKey("q") && isholding == true)
    31.         {
    32.             DropItem();
    33.         }
    34.     }
    35.  
    36.     void OnTriggerStay(Collider other)
    37.     {
    38.         // If the entering collider is a book...
    39.         if (other.gameObject == book && Input.GetKey("e"))
    40.         {
    41.             ScoreManager.currentweight += bookweight;
    42.             other.gameObject.SetActive(false);
    43.             bookimage.color = visible;
    44.             isholding = true;
    45.         }
    46.         // If the entering collider is a map...
    47.         else if (other.gameObject == map && Input.GetKey("e"))
    48.         {
    49.             ScoreManager.currentweight += mapweight;
    50.             Destroy(other.gameObject);
    51.             mapimage.color = visible;
    52.             isholding = true;
    53.         }
    54.         else
    55.         {
    56.         }
    57.     }
    58.  
    59.     void DropItem()
    60.     {
    61.         isholding = false;
    62.         bookimage.color = invisible;
    63.         //book.gameObject.SetActive(true);
    64.         Vector3 position = new Vector3(playerPosition.x + 1, playerPosition.y, playerPosition.z);
    65.         Instantiate(book, position, Quaternion.identity);
    66.     }
    67. }
    I'm still getting this error though :p:

    I'm so sure I'm doing something silly! It's embarrassing haha :D

    By the way, as I'm using Instantiate object, does that mean I can use destroy.gameobject or is gameobject.setactive okay to use?
     
  12. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    You're declaring playerPosition in your Update() function (which you should never do regardless) and then referencing it within another function (DropItem) which has no idea what that variable is.

    In general - error messages tell you everything you need to know. "playerPosition does not exist in the current context" pretty much says it all. You need to move your declaration.

    In general though, you're approaching this the wrong way. You're creating a forward reference to the "player" GameObject, but you only ever use the Transform component of it. Why not just create a reference to the player Transform in the first place and then you're done with all that for the rest of your script, and it has global scope throughout.

    Code (csharp):
    1.  
    2. Transform playerTransform;
    3.  
    4. private void Awake()
    5. {
    6.    playerTransform = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
    7. }
    8.  
    Or along those lines...



    EDIT:

    Just another note, you're declaring a variable called "position" which itself is a keyword. You should always avoid things like that. Call it something else.

    Code (csharp):
    1.  
    2. Vector3 position = new Vector3(playerPosition.x + 1, playerPosition.y, playerPosition.z);
    3.  


    EDIT2:

    Just set your book position "on the fly" while instantiating it. No need to declare that:

    Code (csharp):
    1.  
    2. Instantiate(book, new Vector3(playerTransform.postion.x + 1, playerTransform.position.y, playerTransform.position.z), Quaternion.identity);
    3.  
     
    Last edited: Oct 16, 2015
  13. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Hey Steve,
    Thanks for the advice I appreciate it!
    I am having some problems still, so here is what I have done so far... (with your changes) :)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ObjectPickup : MonoBehaviour
    6. {
    7.     public Image bookimage;
    8.     public Image mapimage;
    9.     public ItemInformation thisItem;
    10.  
    11.     float itemweight;
    12.     bool isholding;
    13.     Color visible = new Color(0f, 0f, 0f, 100f);
    14.     Color invisible = new Color(0f, 0f, 0f, 0f);
    15.  
    16.     GameObject book;
    17.     GameObject map;
    18.     //GameObject player;
    19.     Transform playerTransform;
    20.  
    21.     void Start()
    22.     {
    23.         book = GameObject.FindGameObjectWithTag("book");
    24.         map = GameObject.FindGameObjectWithTag("map");
    25.         //player = GameObject.FindGameObjectWithTag("Player");
    26.         playerTransform = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         //Vector3 playerPosition = player.gameObject.transform.position;
    32.  
    33.         if (Input.GetKey("q") && isholding == true)
    34.         {
    35.             DropItem();
    36.         }
    37.     }
    38.  
    39.     void OnTriggerStay(Collider other)
    40.     {
    41.         // If the entering collider is a book...
    42.         if (other.gameObject == book && Input.GetKey("e"))
    43.         {
    44.             itemweight = 1.2f;
    45.             ScoreManager.currentweight += itemweight;
    46.             other.gameObject.SetActive(false);
    47.             bookimage.color = visible;
    48.             isholding = true;
    49.             //Debug.Log(ItemInformation.itemname,ItemInformation.weight,ItemInformation.description);
    50.         }
    51.         // If the entering collider is a map...
    52.         else if (other.gameObject == map && Input.GetKey("e"))
    53.         {
    54.             itemweight = .2f;
    55.             ScoreManager.currentweight += itemweight;
    56.             other.gameObject.SetActive(false);
    57.             mapimage.color = visible;
    58.             isholding = true;
    59.         }
    60.         else
    61.         {
    62.         }
    63.     }
    64.  
    65.     void DropItem()
    66.     {
    67.         isholding = false;
    68.         bookimage.color = invisible;
    69.         mapimage.color = invisible;
    70.         //book.gameObject.SetActive(true);
    71.         //map.gameObject.SetActive(true);
    72.         Instantiate(book, new Vector3(playerTransform.position.x + 1, playerTransform.position.y + 1, playerTransform.position.z), Quaternion.identity);
    73.     }
    74. }
    Before doing this I was using object.setactive, and I could make the item appear and dissapear on object pick up and drop, just only in the same position.

    However the problems I'm having with this technique now are:

    I pick up the item (object disappears) Thats Good!
    I drop the item (object still is invisible) I can't see the object :(

    EDIT: In the Instantiate function, can I do this? - playerTransform.position.x + 0.55, because I get an error. How can I use floats for more precise positioning.
     
    Last edited: Oct 16, 2015
  14. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    Temp vars such as playerPosition (in the update function) can only be used inside the function they're written in. In this case Update(). To fix that error move it to your DropItem function were its being used, as it only needs to be run through once when the user drops the item.

    For the not being able to see the item when you drop it. You can instantiate the book as a GameObject and access its components through that.

    Example:
    Code (CSharp):
    1.  
    2. GameObject book = Instantiate(book, position, Quaternion.identity) as GameObject;
    3. book.gameObject.SetActive(true);
    4.  
    5. //This is what the DropItem function should look like
    6. void DropItem()
    7. {
    8. isholding = false;
    9. bookimage.color = invisible;
    10.  
    11. Vector3 playerPosition = player.gameObject.transform.position; //Hold the players position in a temp var
    12. Vector3 position = new Vector3(playerPosition.x + 1, playerPosition.y, playerPosition.z); //Grab the players position
    13. GameObject book = Instantiate(book, position, Quaternion.identity) as GameObject; //Instantiate the book as a gameobject and hold it in a temp var for later use
    14. book.gameObject.SetActive(true); //Enable all the components in the gameobject which should make it visible
    15. }
    16.  
    As for the floats you can just put an 'f' next to the number. Example
    Code (CSharp):
    1.  
    2. float x = 0.225f;
    3.  
    For destroying the book or using setActive outside of DropItem you'll need to create a variable to hold it in as you can't access 'book' outside of DropItem. You could do this by storing it in an array. Theres probably a better way but an array should work.

    Code (CSharp):
    1.  
    2. public GameObject[] playerItemDropped;
    3. ...
    4.  
    5. void DropItem()
    6. {
    7. isholding = false;
    8. bookimage.color = invisible;
    9.  
    10. Vector3 playerPosition = player.gameObject.transform.position; //Hold the players position in a temp var
    11. Vector3 position = new Vector3(playerPosition.x + 1, playerPosition.y, playerPosition.z); //Grab the players position
    12. GameObject item = Instantiate(book, position, Quaternion.identity) as GameObject; //Instantiate the book as a gameobject and hold it in a temp var for later use
    13. item.gameObject.SetActive(true); //Enable all the components in the gameobject which should make it visible
    14.  
    15. //Check through the array to find the next available spot.
    16. for(int i = 0; i < playerItemsDropped.legnth; i++)
    17. {
    18.      if(playerItemsDropped[i] == null)
    19.           playerItemsDropped[i] = item; //This should allow you to access the instantiated book object outside of the        dropItems function.
    20.           break;
    21.      }
    22. }
    23. }
    24.  
    Hopefully this made sense, :D
     
    Hotpots likes this.
  15. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Hey Geek!
    All of it made clear sense except the last part (the array), so thanks for your explanation! :p

    I have this so far:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ObjectPickup : MonoBehaviour
    6. {
    7.     public Image bookimage;
    8.     public Image mapimage;
    9.     public ItemInformation thisItem;
    10.  
    11.     float itemweight;
    12.     bool isholding;
    13.     Color visible = new Color(0f, 0f, 0f, 100f);
    14.     Color invisible = new Color(0f, 0f, 0f, 0f);
    15.  
    16.     GameObject book;
    17.     GameObject map;
    18.     GameObject player;
    19.     //public GameObject[] playerItemsDropped;
    20.     //Transform playerTransform;
    21.  
    22.     void Start()
    23.     {
    24.         book = GameObject.FindGameObjectWithTag("book");
    25.         map = GameObject.FindGameObjectWithTag("map");
    26.         player = GameObject.FindGameObjectWithTag("Player");
    27.         //playerTransform = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
    28.     }
    29.  
    30.     void Update()
    31.     {
    32.         if (Input.GetKey("q") && isholding == true)
    33.         {
    34.             DropItem();
    35.         }
    36.     }
    37.  
    38.     void OnTriggerStay(Collider other)
    39.     {
    40.         // If the entering collider is a book...
    41.         if (other.gameObject == book && Input.GetKey("e"))
    42.         {
    43.             itemweight = 1.2f;
    44.             ScoreManager.currentweight += itemweight;
    45.             other.gameObject.SetActive(false);
    46.             bookimage.color = visible;
    47.             isholding = true;
    48.             //Debug.Log(thisItem.itemname,thisItem.weight,thisItem.description);
    49.         }
    50.         // If the entering collider is a map...
    51.         else if (other.gameObject == map && Input.GetKey("e"))
    52.         {
    53.             itemweight = .2f;
    54.             ScoreManager.currentweight += itemweight;
    55.             other.gameObject.SetActive(false);
    56.             mapimage.color = visible;
    57.             isholding = true;
    58.         }
    59.         else
    60.         {  
    61.         }
    62.     }
    63.  
    64.     void DropItem()
    65.     {
    66.         if (bookimage.color == visible)
    67.         {
    68.             itemweight = 1.2f;
    69.             ScoreManager.currentweight -= itemweight;
    70.         }
    71.         if (mapimage.color == visible)
    72.         {
    73.             itemweight = .2f;
    74.             ScoreManager.currentweight -= itemweight;
    75.         }
    76.         isholding = false;
    77.         bookimage.color = invisible;
    78.         //mapimage.color = invisible;
    79.         Vector3 playerPosition = player.gameObject.transform.position;
    80.         Vector3 position = new Vector3(playerPosition.x + 4, playerPosition.y + 0.55f, playerPosition.z);
    81.         GameObject item = Instantiate(book, position, Quaternion.identity) as GameObject;
    82.         item.gameObject.SetActive(true);
    83.         //map.gameObject.SetActive(true);
    84.         //Instantiate(book, new Vector3(playerTransform.position.x + 1, playerTransform.position.y + 1, playerTransform.position.z), Quaternion.identity);
    85.         //Check through the array to find the next available spot.
    86.     }
    87. }
    And what happens is:
    It creates the game object whenever I drop it
    However it sometimes drops it to the side of the player or in front, its completely random, do you know why?
    Lastly, I can't pick the item back up, is that because the instantiated item does not have the "book" tag?
     
  16. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    If the tag of the item needs to be book what you can do is under line 82
    Code (CSharp):
    1.  
    2. item.gameObject.SetActive(true);
    3.  
    4. //You can write this to change the tag
    5. item.gameObject.tag = "book";
    6. //This will only work if the tag 'book' is added to the list of tags
    7.  
    As for the problem with the book spawning to the side, you can do a check to which way the player is facing using Vector3.forward. I found this out just before hah.

    Code (CSharp):
    1.  
    2. //Line 80 and 79 can be removed, but its best to just comment them out just in case it doesn't work.
    3. GameObject item = Instantiate(book, Vector3.forward, Quaternion.identity) as GameObject;
    4. ...
    5.  
    Heres the link to the reference for Vector3.forward
    http://docs.unity3d.com/ScriptReference/Vector3-forward.html

    Hopefully this solves it ;)
     
  17. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Hey Geek!
    So I tried Vector3.forward, and this was the outcome (sorry for the white on white :p):
    The cube still spawns behind the player and I still can't pick it up :(

    Usually in games I've developed before I only ever have objects that you collect for points and thus I destroy the object as it doesn't matter. So this is the first time for me... trying to bring the objects "back to life" or reinstantiated. I appreciate your help Geek :)



    This is my code for the dropitem():
    Code (CSharp):
    1.  
    2.     void DropItem()
    3.     {
    4.         if (bookimage.color == visible)
    5.         {
    6.             itemweight = 1.2f;
    7.             ScoreManager.currentweight -= itemweight;
    8.         }
    9.         if (mapimage.color == visible)
    10.         {
    11.             itemweight = .2f;
    12.             ScoreManager.currentweight -= itemweight;
    13.         }
    14.         isholding = false;
    15.         bookimage.color = invisible;
    16.         //mapimage.color = invisible;
    17.         //Vector3 playerPosition = player.gameObject.transform.position;
    18.         //Vector3 position = new Vector3(playerPosition.x + 2, playerPosition.y + 0.55f, playerPosition.z + 2);
    19.         //GameObject item = Instantiate(book, position, Quaternion.identity) as GameObject;
    20.         GameObject item = Instantiate(book, Vector3.forward, Quaternion.identity) as GameObject;
    21.         item.gameObject.SetActive(true);
    22.         item.gameObject.tag = "book";
    23.         //map.gameObject.SetActive(true);
    24.         //Instantiate(book, new Vector3(playerTransform.position.x + 1, playerTransform.position.y + 1, playerTransform.position.z), Quaternion.identity);
    25.     }
    26. }
    27.  
     
    Last edited: Oct 18, 2015
  18. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    Hmmm. Okay. I see, I have one idea that may work if I explain it correctly :D

    The idea is to just create an empty game object that's a child of the player (so it follows the player) and sits in front of the player, and is used as a spawn point for the item, basically forcing the item to spawn in front of the player. You should be able to use

    Code (CSharp):
    1.  
    2. /* What to do:
    3. Create an empty
    4. Attach it to the player
    5. Move it in front of the player
    6. */
    7.  
    8. Vector3 position = GameObject.find("Name Of The Spawn Point Attached To The Player").transform.position; //Find the spawn point in the scene and grab its position
    9.  
    10. //So this is what the instantiate would look like with the added code
    11. GameObject item = Instantiate(book, position, Quaternion.identity) as GameObject;
    12.  
    If this doesn't work then I am out of ideas, sorry!
    Good luck :)
     
  19. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    It DID work geek so thank you very much, the object spawns in the perfect position! :)
    Only small problem now is that although the object is instantiated I can't pick it back up.

    My question is once we instantiate the item, book, does that gameobject have the "book" tag?
    As my educated guess would possibly be in my if statement for picking up the item. If tag = "book" and input key = "e".
     
  20. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    So my guess was correct! :)

    Code (CSharp):
    1.         // If the entering collider is a book...
    2.         if (/*other.gameObject == book &&*/ Input.GetKey("e"))
    3.         {
    4.             itemweight = 1.2f;
    5.             ScoreManager.currentweight += itemweight;
    6.             other.gameObject.SetActive(false);
    7.             bookimage.color = visible;
    8.             isholding = true;
    9.             //Debug.Log(string.Format("{0} {1} {2}", thisItem.itemname, thisItem.weight, thisItem.description));
    10.         }
    11.         // If the entering collider is a map...
    12.         else if (other.gameObject == map && Input.GetKey("e"))
    13.         {
    14.             itemweight = .2f;
    15.             ScoreManager.currentweight += itemweight;
    16.             other.gameObject.SetActive(false);
    17.             mapimage.color = visible;
    18.             isholding = true;
    19.         }
    When I comment out
    Code (CSharp):
    1. other.gameObject == book &&
    everything works perfectly (picking up and dropping items in front of the player) other than detecting the correct items.

    So by using instantiate how can I get it to instantiate the book item with the "book" tag after dropping and creating another instance of book?

    Totally stuck about that! :p
     
  21. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    That's great news :D

    To give an instantiated item a tag just use the code:

    Code (CSharp):
    1.  
    2. GameObject item = Instantiate(book, position, Quaternion.identity) as GameObject;
    3. item.gameObject.tag = "book";
    4.  
    Good luck :D
     
    Hotpots likes this.
  22. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Thanks for all your help geek, everything is working as intended.

    Items are being picked up by checking gameobject tags and user input. If the user drops an item, it is instantiated in front of the player relevant to the position of the CollectableSpawnPoint emptygameobject.

    Here is the solution to my problem, for future reference and googlers: :)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ObjectPickup : MonoBehaviour
    6. {
    7.     public Image bookimage;
    8.     public Image mapimage;
    9.     //public ItemInformation thisItem;
    10.     public static float itemweight;
    11.  
    12.     bool isholding;
    13.     Color visible = new Color(0f, 0f, 0f, 100f);
    14.     Color invisible = new Color(0f, 0f, 0f, 0f);
    15.  
    16.     GameObject book;
    17.     GameObject map;
    18.     GameObject player;
    19.     //public GameObject[] playerItemsDropped;
    20.  
    21.  
    22.     void Start()
    23.     {
    24.         book = GameObject.FindGameObjectWithTag("book");
    25.         map = GameObject.FindGameObjectWithTag("map");
    26.         player = GameObject.FindGameObjectWithTag("Player");
    27.         //anim = GetComponent<Animator>();
    28.     }
    29.  
    30.  
    31.     void Update()
    32.     {
    33.         if (Input.GetKey("q") && isholding == true)
    34.         {
    35.             DropItem();
    36.         }
    37.     }
    38.  
    39.  
    40.     void OnTriggerStay(Collider other)
    41.     {
    42.         // If the entering collider is a book...
    43.         if (other.gameObject.tag == "book" && Input.GetKey("e"))
    44.         {
    45.             itemweight = 1.2f;
    46.             ScoreManager.currentweight += itemweight;
    47.             other.gameObject.SetActive(false);
    48.             bookimage.color = visible;
    49.             isholding = true;
    50.             //Debug.Log(string.Format("{0} {1} {2}", thisItem.itemname, thisItem.weight, thisItem.description));
    51.         }
    52.         // If the entering collider is a map...
    53.         else if (other.gameObject.tag == "map" && Input.GetKey("e"))
    54.         {
    55.             itemweight = .2f;
    56.             ScoreManager.currentweight += itemweight;
    57.             other.gameObject.SetActive(false);
    58.             mapimage.color = visible;
    59.             isholding = true;
    60.         }
    61.         else
    62.         {
    63.         }
    64.     }
    65.  
    66.        
    67.     void DropItem()
    68.     {
    69.         if (bookimage.color == visible)
    70.         {
    71.             itemweight = 1.2f;
    72.             ScoreManager.currentweight -= itemweight;
    73.             bookimage.color = invisible;
    74.             Vector3 position = GameObject.Find("CollectableSpawnPoint").transform.position;
    75.             GameObject item = Instantiate(book, position, Quaternion.identity) as GameObject;
    76.             item.gameObject.tag = "book";
    77.             item.gameObject.SetActive(true);
    78.         }
    79.         if (mapimage.color == visible)
    80.         {
    81.             itemweight = .2f;
    82.             ScoreManager.currentweight -= itemweight;
    83.             mapimage.color = invisible;
    84.             Vector3 position = GameObject.Find("CollectableSpawnPoint").transform.position;
    85.             GameObject item = Instantiate(map, position, Quaternion.identity) as GameObject;
    86.             item.gameObject.tag = "map";
    87.             item.gameObject.SetActive(true);
    88.         }
    89.         isholding = false;
    90.     }
    91. }
     
    GeekStories likes this.