Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Instantiate cloning problem

Discussion in 'Scripting' started by subject0Delta, Sep 24, 2022.

  1. subject0Delta

    subject0Delta

    Joined:
    Jan 2, 2021
    Posts:
    2
    I am having trouble with the Instantiate command. I have a script that is supposed to replace the object what it is a part of with anther, and it works! put for a little bit because it is creating clones and then clones of clones and so on and so forth until it no longer can place them
    clone.png
    and this is probably the culprit
    culprit .png
    I don't know why this is happening
    put here is the code
    Code (CSharp):
    1. using Unity.Mathematics;
    2. using UnityEngine;
    3.  
    4. public class Lever_slfedistract : MonoBehaviour
    5. {
    6.     public GameObject ON;
    7.     public GameObject OFF;
    8.     private Vector3 vector3;
    9.     private quaternion quaternion;
    10.     private void Awake()
    11.     {
    12.         vector3 = transform.position;
    13.         quaternion = transform.rotation;
    14.     }
    15.     public void Lever_ON()
    16.     {
    17.         Instantiate(OFF,vector3,quaternion);
    18.         gameObject.SetActive(false);
    19.     }
    20.     public void Lever_OFF()
    21.     {
    22.         Instantiate(ON, vector3, quaternion);
    23.         gameObject.SetActive(false);
    24.     }
    25. }
    what I am trying to do is to make it flip back and forth between the two prefabs every time the player click on them and the click detection is done but I am have trouble with the flipping. ty
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    Why Instantiate? Why not just have an ON and an OFF lever, put them in the scene, drag references to them into your script and set them active / inactive when you want?
     
  3. subject0Delta

    subject0Delta

    Joined:
    Jan 2, 2021
    Posts:
    2
    thank for commenting. it's because I am going to make anther script that counts how many are on and how many are off so it increases or decrease the points accordingly; and also because I am new to game development so I'm not very experienced on what to do or in the various commands
    so if you have any suggestions or tips i wood love to hear them.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    I'd make a lever script, make a prefab out of it that contains both on and off, put it in a blank scene and test it until the script works.

    Then apply the prefab, go to your main scene and put as many of them around as you want.

    Once you've gone that far you can make a script that counts them and asks if they're tripped on or off easily enough... tons of tutorials on basic C# list handling.