- Home /
Is there an alternative way to make an object disappear but still use its script
Each item has a different script and when picked up they give the player all kinds of bonuses. Some of them even give the player passive abilities such as reflective damage. However i cant just delete the game object of the item, since that will also erase it's script and yet i still want it to disappear from the view. My way of doing so is by disabling it's SR and box collider, but i feel like that's not the best solution..
Answer by VReindeer · Feb 16 at 09:30 PM
Only 2 solutions I can think of is setting the object as inactive after you pick it up and it provides the player's bonus. The other which I personally would rather do, is that you attach the script to an empty/invisible but active game object, and make the object a child of it. Add the object as a GameObject variable to the script and set it to be destroyed or inactive after the condition/bonus has been made.
Ok, so the GameObject which will hold the item's BoxCollider and sprite will belong to a parent object which will hold the script. When the player picks up the item, the child object will be destroyed but the parent, holding the script, will still stay active. Correct me if I misunderstood you.
Ok, thanks a lot! I just wanted to make sure that I'm doing things right. The reason I'm asking this question is to make my game as optimized as possible!
Answer by Qaraman · Feb 16 at 07:34 PM
Search for (C# Object Oriented Programing) tutorials and learn how to use a script (a Class) of another script (another class).
I do know how to reference a script inside another script, unfortunately that doesn't really work in this situation. Example for this is the item that reflects damage back to the attacker each time the player takes some since i don't want my PlayerHealth script to get overloaded with many functions and variables and in this case references, instead im using the event system, which will call the "playerHasTakenDamage" event which will trigger the ability of my damage reflection item. If the item is deleted from the scene, the event wont be able to trigger the item's function and therefore nothing will happen. Perhaps using the event system isn't the right call here but i still disagree that i should be referencing the item in the PlayerHealth script
Your answer
Follow this Question
Related Questions
One script for many Game Objects with different values 0 Answers
Adding an unkown script dynamically 0 Answers
Need help for Translating a peg in the game... 1 Answer
Is there a way to dynamically attach a script to a GameObject during runtime? 1 Answer
How Can i Fade In a GameObject with LeanTwean plugin? 1 Answer