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

Using another script to Destroy and Instantiate an Object

Discussion in 'Scripting' started by saberwolfcdw, Oct 23, 2015.

  1. saberwolfcdw

    saberwolfcdw

    Joined:
    Sep 21, 2015
    Posts:
    14
    I am needing to use script not directly connected to an object to destroy the Object and recreate it where it is just like the beginning of the game. This is a block breaker game where the ball starts on the paddle and you click to start the ball moving. I am creating a Lives system to work with it.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. //using UnityEngine.Object;
    5.  
    6. public class LifeCollider3D : MonoBehaviour {
    7.     public int lifeCount;
    8.     //public UnityEngine.Object Ball3D;
    9.     public Transform Ball3D;
    10.     public Paddle3D paddle3D;
    11.     public LevelManager levelManager;
    12.  
    13.  
    14.     void OnTriggerEnter (Collider trigger){
    15.         lifeCount = lifeCount - 1;
    16.         if (lifeCount > 0) {
    17.             print ("Ball Lost, Life Lost");
    18.             UnityEngine.Object.DestroyImmediate (Ball3D);
    19.             UnityEngine.Object.Instantiate (Ball3D, new Vector3(0,0,0));
    20.         }
    21.         else {levelManager.LoadLevel("Lose");
    22.         }
    23.     }
    24.  
    25. }
    26.  
     
  2. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,209
    Why don't you just reposition it?

    Another way is to make your public variable(A) and fill it up but don't instantiate it.
    Then, make another variable(B), public or private, and use that for your instantiate. The original(A) will always have the original fresh version stored in it. When you instantiate or destroy your (B) you're just working with a copy of it. I do that all the time for spawning lists of buttons in my UI. Store the master copy, then instantiate it a bunch of times when needed.
     
  3. saberwolfcdw

    saberwolfcdw

    Joined:
    Sep 21, 2015
    Posts:
    14
    I'm sorry. I am very new to coding. I fine with just re-positioning the ball but how do I get it to re-connect to the paddle so it can be fired again from the paddle to start the next life. I thought it would be easier to get the new instance to do that since that it is how it starts from the beginning. Could a see an example code that would do what you are suggesting? I seem to understand better when I can see how the code is written.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    One thing to note, do NOT use DestroyImmediate - use Destroy. DestroyImmediate will cause problems if you're not certain of exactly how and when it's supposed to be used.

    I don't know how it's connected in the first place, so I can't really help you here. Is there a script somewhere? Is it connected as a joint or something?
     
  5. saberwolfcdw

    saberwolfcdw

    Joined:
    Sep 21, 2015
    Posts:
    14
    ok right. There a script on the ball that sets the Y vertex on the ball so it matches the paddle. Then when the mouse is clicked it sends the ball flying up. I'll include both the ball and paddle scripts for references.

    Paddle Script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Paddle3D : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.      
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.         Vector3 paddlePos = new Vector3 (0.5f, this.transform.position.y, 0f);
    15.      
    16.         float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
    17.         //print("Paddle Pos " + mousePosInBlocks);
    18.      
    19.         paddlePos.x = Mathf.Clamp(mousePosInBlocks, -1f, 17f);
    20.      
    21.         this.transform.position = paddlePos;
    22.     }
    23. }
    24.  
    Ball Script
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Ball3D : MonoBehaviour {
    6.  
    7.     private Paddle3D paddle3D;
    8.     public bool started = false;
    9.  
    10.     public Vector3 paddleToBallVector;
    11.  
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         paddle3D = GameObject.FindObjectOfType<Paddle3D>();
    16.         paddleToBallVector = this.transform.position - paddle3D.transform.position;
    17.         print ("Paddle to ball Vector = " + paddleToBallVector.y);
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update () {
    22.         if (!started){
    23.             Vector3 ballPos = new Vector3 (this.transform.position.x, this.transform.position.y, this.transform.position.z);
    24.          
    25.             //Locks ball to paddle
    26.             this.transform.position = paddle3D.transform.position + paddleToBallVector;
    27.          
    28.             //print ("Paddle to ball Vector = " + paddleToBallVector.y);
    29.          
    30.             //Launchs ball on mouse click
    31.             if(Input.GetMouseButtonDown(0)){
    32.                 print("Mouse 1 Clicked, Launch Ball");
    33.                 this.GetComponent<Rigidbody>().velocity = new Vector3 (0f,12f, 0f);
    34.                 started = true;
    35.             }
    36.             //ballPos.z = Mathf.Clamp(-1f, -1.2f);
    37.         }
    38.     }
    39. }
     
  6. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,209
    in the code you provided you are setting its vector3 to zero which in world space would be the global zero, or in local space (for children) it would be the zero of the parent. In Unity the only way things are actually "connected" is if they have a parent-child relationship or if there is code somewhere saying make positionA the same as positionB every frame.

    for example the leaves on a tree are a child object to the tree. that way when the tree is told to move or sway, the leaves don't actually move their own local position, they move by keeping their local position to zero which moves with the parent position.

    for you case, sounds like pong, I would do something like this:
    Code (CSharp):
    1. public class PaddleBall : MonoBehaviour {
    2.  
    3.     //a reference to the paddle
    4.     Transform paddle;
    5.  
    6.     void Start () {
    7.         //find it once at the start
    8.         paddle = GameObject.Find("Paddle").transform;
    9.     }
    10.  
    11.     public void BallLost () {
    12.         //call this function from outside to reposition the ball to set the ball just above the paddle
    13.         transform.position = new Vector3(paddle.position.x, paddle.position.y + 0.5f, paddle.position.z);
    14.     }
    15.  
    16. }
    17.  
    18. public class Catcher : MonoBehaviour {
    19.  
    20.     public GameObject paddleBall;
    21.     public int lifeCount;
    22.  
    23.     void Start () {
    24.         PaddleBall = GameObject.Find("PaddleBall");
    25.     }
    26.  
    27.     void OnTriggerEnter (Collider trigger) {
    28.         lifeCount = lifeCount - 1;
    29.         if (lifeCount > 0) {
    30.             print ("Ball Lost, Life Lost");
    31.             paddleBall.GetComponent<PaddleBall>().BallLost();
    32.         }
    33.         else {
    34.             Application.LoadLevel("Lose");
    35.         }
    36.     }
    37.  
    38. }
    also as a tip, you should decide on a system for how you want to capitalize and name you variables. For example don't capitalize any of your variables, because you always capitalize your scripts. I like to put an underscore before all my public variables so they show up when I access them from outside as soon as I press _. some people do privates with a _ etc. makes like easier.
     
  7. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,209
    when I posted this yesterday you're third post with the scripts hadn't shown up yet =o what's up with that??? I don't have time to read through it right now sorry. My second post might be out of place though...
     
  8. saberwolfcdw

    saberwolfcdw

    Joined:
    Sep 21, 2015
    Posts:
    14
    That helps a lot Malkere! thanks alot!
     
    malkere likes this.