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. Dismiss Notice

I only want to destroy the instantiate gameObject and not all of the prefabs of the gameObject.

Discussion in 'Scripting' started by Reda2, Aug 4, 2014.

  1. Reda2

    Reda2

    Joined:
    Jul 25, 2014
    Posts:
    37
    So I have 3 different prefabs of different balls that spawn into the screen at random times, and when the user touches a ball, the ballValue of the ball decreases by one and when the ballValue is equal to 0 I want to destroy that particular gameObject that the user pressed, but it destroys all the other gameObjects that are in the world that came from the same prefab. I don't want it to destroy all the spawned gameObjects of the same prefab, I only want to destroy the gameObject that the user pressed.

    If you didn't understand please ask me and I will explain further.

    Here is code:

    Code (CSharp):
    1. public GUIText BallText;
    2. public int BallValue;
    3. public int BallScore;
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Input.GetMouseButtonDown (0))
    4.         {
    5.             Vector2 pos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    6.             RaycastHit2D hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(pos), Vector2.zero);
    7.  
    8.             if (hitInfo == false)
    9.             {
    10.             }
    11.  
    12.             else if (hitInfo.collider.gameObject.name == gameObject.name)
    13.             {
    14.                 BallValue -= 1;
    15.                 BallText.text = BallValue.ToString();
    16.  
    17.                 if (BallText.text == "0")
    18.                 {
    19.                     Destroy(gameObject);
    20.                     GameController.score += BallScore;
    21.                 }
    22.             }
    23.         }
    24.     }
     
  2. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    First of all you should out the line, "GameController.score += BallScore;" before the Destroy function because it won't be called...an object that doesn't exist can run code. Also are you dragging the ball prefab from your scene or project? If its from your scene, then when the object that your scripts are referencing is destroyed, it'll destroy all the others I think
     
  3. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Oh and you should use the proper naming convention for variables. the first word always stays lowercase.
    Example
    Code (csharp):
    1.  
    2. //What you are doing:
    3. var VariableName : GameObject;
    4. var Object : GameObject;
    5. var Score : float;
    6. var PlayersCameraSpeed : float;
    7. //What you should do:
    8. var variableName : GameObject;
    9. var object : GameObject;
    10. var xscore : float;
    11. var playersCameraSpeed : float;
    Sorry that this is in javascript, I just don't like C# and the translation isn't hard at all
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That's actually against the C# standard. Public variables are Pascal Case, private/protected are Camel Case.

    To OP - you'll want to destroy hitInfo.collider.gameObject instead.
     
    Reda2 likes this.
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Actually the C# standard is that properties should be PascalCase, and public instance variables should be camelCase, however public variables are discouraged from being used at all. That doesn't really apply to Unity since it does use public instance variables for the inspector. Also it differs from the Unity standard of using camelCase for properties, but honestly that probably wasn't the best decision.

    --Eric
     
  6. Reda2

    Reda2

    Joined:
    Jul 25, 2014
    Posts:
    37

    In that line I'm referring to a GUIText called "score" in another script, I'm using public static, which makes it use able in any script. That's not my problem, my problem is the destroy line.
     
  7. Reda2

    Reda2

    Joined:
    Jul 25, 2014
    Posts:
    37

    That actually fixed the problem, thanks a lot. But now I have a second problem, when I press a ball, the BallValue for all other gameObjects that are the same prefab changes as well, whereas I only want the ball that I touch decrease it's BallValue.
     
  8. Reda2

    Reda2

    Joined:
    Jul 25, 2014
    Posts:
    37
    So you guys want me to use "ballValue", "ballText" and "ballScore" instead?
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    That would be best...it's useful to have a consistent method to distinguish variables from properties, since with variables you know that what you put in is what you get out; with properties, not so much. (Which is why the Unity convention of using camelCase for both was not such a great idea.)

    --Eric
     
  10. Reda2

    Reda2

    Joined:
    Jul 25, 2014
    Posts:
    37
    So can you help me with my problem please? My initial problem is fixed but now when I press a ball, the BallValue for all other gameObjects that are the same prefab changes as well, whereas I only want the ball that I touch decrease it's BallValue.
     
  11. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    It seems like you need to just watch tutorials and stuff to learn the basic parts of coding. Its kind of not helpful to you if you come here with a problem the moment it happens. Look up your issue and see if its already covered and look over it yourself.
     
  12. Reda2

    Reda2

    Joined:
    Jul 25, 2014
    Posts:
    37
    Yes I do that, I didn't come here straight away, I have been trying everything. I assume that people will not answer immediately so in case I don't find a solution I can check on the forum post. And I think I know the basics of coding, please don't judge just by a single script.
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    I guess ballText refers to the prefab itself? You'd want to refer to prefab instances instead. A prefab should only be referenced in order to instantiate it, and everything you do afterwards should be done to the instances rather than the prefab.

    --Eric
     
  14. Reda2

    Reda2

    Joined:
    Jul 25, 2014
    Posts:
    37
    The BallText variable is a GUIText that is a child object to the prefab, what I want is that text to decrease by 1 only for that game object that the player presses, because right now when I press a ball that was instantiated from the prefab, all of the game objects GUIText from the same type of prefab changes as well because there are multiple that spawn.

    Thanks for any help you can give me mate, I really do appreciate it.