- Home /
how to destroy object permanantly throught c#
Im creating a button in my game where the player is able to subscribe my YouTube channel and earn coins. So when i click on the the subscribe button and i earn coins but when i restart the scene, The subscribe button in the game appears again. The subscribe button is supposed to be clicked once because the player can only subscribe my channel once. So basically i want to call a function and say the object should destroy permantly and must save.
Answer by etaxi341 · Aug 30, 2016 at 03:14 PM
I would recommend you to check if the guy is subscribed to your channel and only if not enable the gameobject.
Or another method could be that you save in the PlayerPrefs a bool when the button has been pressed. When this bool is true the button is not there.
You can just hide the button by adding a script to it which checks OnEnable if the bool is true and then set the Gameobject Inactive
Thanks for your quick reply. I understand the logic but I don't know how to code. Would you please help me with the code as I'm new to coding. It would be a great help! @etaxi341
Ok so I will show you how to do it with PlayerPrefs :) So first the ClickButton method gets called when you press the Button (You have to set it to the button by yourself)
The Start $$anonymous$$ethod wil be called when the Object should be loaded. While loading the Object it will check if the button was clicked already. if yes then hide the gameobject.
void ClickButton()
{
PlayerPrefs.setInt("clickedYoutube", 1);
PlayerPrefs.Save();
//And here open your youtube
HideButton();
}
void Start()
{
HideButton();
}
void HideButton()
{
if (PlayerPrefs.getInt("clickedYoutube") == 1)
gameObject.enabled = false; // This disables the gameobject if the player already clicked once at the button.
}
Answer by trevorchico · Aug 31, 2016 at 05:40 AM
http://answers.unity3d.com/questions/726178/how-do-i-use-playerprefs-to-save-my-score.html
PlayerPrefs will be the easiest, you can find example code on the unity forums like this one^
Thanks for your reply! As I said, I'm new to coding. Would you $$anonymous$$d completing the code for me with the load level code above! This is a great help as I'm in the final stage of publishing my game!
Answer by Thanish-ahmed · Aug 31, 2016 at 07:54 AM
Hey, I really appreciate your work, But things dont seem to be good. I have attached my code below ( Load URL CODE). Currently im having a button and when i click the button the url opens successfully. Please combine your code with mine and reply. Thanks!
using UnityEngine;
namespace Zigzag {
public class ZIRLoadLevel : MonoBehaviour
{
public void LoadURL(string urlName)
{
Application.OpenURL(urlName);
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Destroy an enemy without destroying the player 1 Answer
Help with Spawning & Destroying Background Objects in 2D 2 Answers
Instantiating Objects 1 Answer