Can you get a value from a object when destroyed?
I want to know if there is any way to get a value assigned to a box but only when the box is destroyed. Using Destroy(other.gameObject);
Sure you can but I think we all need to know which value you want to get. What do you want to get from the box?
there will be different values on boxes but it will only give out points that is set to the box
Answer by SpaceManDan · Oct 06, 2019 at 01:35 AM
I'm going off concept of execution here since I don't have any real details. Here is an example of one way this can be done.
Create a GameObject and name it BoxOfPoints. Now add a new tag to your project in tags and layers and call that tag "BoxPoints".
Now create a new script and call it "BoxInfo". FIRST, add the script to the new box you just created. The box called BoxOfPoints should now have an empty script on it called BoxInfo. Open the script BoxInfo for editing and put the following code in it.
public class BoxInfo : MonoBehaviour
{
public int boxPoints;
private void Start()
{
transform.tag = "BoxPoints";
boxPoints = 10;
}
}
Now, create a new script and name it PlayerPoints. Add it to the player. You should now have an empty script called PlayerPoints on the players gameObject. Open the script PlayerPoints for editing and add the following code.
public class PlayerPoints : MonoBehaviour
{
public int playersBoxPoints;
private void OnCollisionEnter(Collision collision)
{
if(collision.transform.tag == "BoxPoints")
{
BoxInfo boxInfo = collision.transform.GetComponent<BoxInfo>();
playersBoxPoints += boxInfo.boxPoints;
Destroy(boxInfo.gameObject);
}
}
}
Now, duplicate the box so you have like 5 or 6 of them all in different locations. Every time the player collides with the box, it will add 10 points to the players points and destroy that instance of the box gameobject. Now keep in mind, you could do this from any object, like an arrow shot from a bow could feed the points to the player... or anything really. But just for a concept, this should get you thinking in the right direction. Now if you want to share your code with us. I can be WAY more specific because you can do something like this literally thousands of ways.
Hope this helps.
i don't have time to test it today but at a later date ill check.
thank you.
Answer by Mokomaki · Oct 05, 2019 at 09:33 AM
Well thats vague... But couldent you just send the value out right before you destroy it?
there will be different values on boxes but it will only give out points that is set to the box
Ok so do you destroy the box from its own script or another script ?` I would have a script on the box that could hold the number of points it will give and right before you destroy it you could just usee the same reference you use to destroy it in the first place to get the variable that holds the number of points. I would suggest sharing the bit of code where you destroy the box.
Hope you find what you need :)
Your answer
Follow this Question
Related Questions
Objects getting destroyed in unity test, but only sometimes in the phone app 0 Answers
Problem after 5 seconds destroy/deactivate component 1 Answer
Simple call animation then destroy? 0 Answers
How do i add blinking effect to an instantiated object ? 0 Answers
Destroying an object in a matrix 0 Answers