Home c# Many objects are spawned instead of one unity 3D

Many objects are spawned instead of one unity 3D

Author

Date

Category

This is such a problem:
Created an empty object “Cube Spawner” and attached the “Cubespawner” script to it:

using system.collections;
Using System.Collections.Genic;
using unityengine;
Public Class Cubespawner: Monobehaviour
{
  Public GameObject Cube;
  bool Ready = false;
  Transform POS;
  Public Float Delay;
  Private Void Start ()
  {
    POS = getComponent & lt; transform & gt; ();
    StartCoroutine (spawncube ());
    Instantiate (Cube);
  }
  Private Void OntrigGeNTER (Collider Other)
  {
    if (Other.tag == "Cube")
    {
      ready = false;
    }
  }
  Private Void Ontriggerstay (Collider Other)
  {
    if (Other.tag == "Cube")
    {
      ready = false;
    }
  }
  Private Void OnTriggereExit (Collider Other)
  {
    if (Other.tag == "Cube")
    {
      READY = TRUE;
      Instantiate (Cube);
    }
  }
  Ienumerator SPAWNCUBE ()
  {
    Yield Return New WaitForSeconds (0.1f);
    If (Ready == True)
    {
      float xpos = pos.position.x;
      float ypos = pos.position.y;
      float zpos = pos.position.z;
      ready = false;
      Instantiate (Cube, New Vector3 (XPOS, YPOS, ZPOS), quaternion.identity);
    }
    StartCoroutine (spawncube ());
  }
}

At the beginning, I have one cube (everything is going normally) with the animation when I press the “w” cube and triggers a trigger, the disappearance of the object with the spawner. Next appears 2 cube with animation, after pressing “W” and more, and more …

Here is the prefab script itself:

using system.collections.Genic;
using unityEditor.scenemanagement;
using unityengine;
Public Class Cubecontroller: Monobehaviour
{
  BOOL _ISPLAYABLE;
  Rigidbody RB;
  Private Void Start ()
  {
    _isplayable = true;
    rb = getComponent & lt; rigidbody & gt; ();
  }
  Private void Update ()
  {
    Cubereaction ();
  }
  Private Void CubereAction ()
  {
    if (_isplayable == True & amp; & amp; input.getkeydown ("w"))
    {
      _isplayable = false;
      RB.ISKINEMATIC = FALSE;
      Destroy (GetComponent & LT; Animator & GT; ());
    }
  }
}

Need to spawned one cube, can you help?


Answer 1, Authority 100%

Scheme with collides and tags slim, confusing and not necessary.

[disallowmultiplecomponent]
Public Class Cubespawner: Monobehaviour
{
  // field internal kitchens should not be public
  // SeryalizeField fields are reflected in the inspector
  [SerializeField] Private Cube _CUBETEMPLATE;
  // How many Cuba need to fly to the next Savna
  [SerializeField] Private Float _respawndeltay = 1f;
  [SerializeField] Private Keycode _FallKey = Keycode.w;
  Private transform _transform;
  Private Cube _CurrentCube;
  Private Void Awake ()
  {
    _Transform = transform;
    Spawncurrentcube ();
  }
  Private void Update ()
  {
    If (_CURRENTCUBE! = NULL & AMP; & amp; input.getKeydown (_FallKey))
    {
      _CurrentCube.fall ();
      STARTCOROUTINE (_CURRENTCUBE.TRANSFORM));
      _CURRENTCUBE = NULL;
    }
  }
  Private Ienumerator RespawnCubecoroutine (TRANSFORM TARGET)
  {
    While (True)
    {
      if (target.position.y & lt; _transform.position.y-_respawndeltay)
        Break; // out of the cycle
      Yield Return NULL; // Each frame
    }
    Spawncurrentcube ();
  }
  Private Void SpawnCurrentCube ()
  { 
GameObject NewCube = Instantiate (_CubeTemplate.GameObject);
     Newcube.transform.position = _Transform.position;
     _CurrentCube = newCube.getcomponent & lt; cube & gt; ();
   }
}

[disallowmultiplecomponent]
[RequireComponent (TypeOf (Rigidbody)]
Public Class Cube: Monobehaviour
{
   Private Rigidbody _Body;
   Private Void Awake ()
   {
     _body = getComponent & lt; rigidbody & gt; ();
   }
   Public Void Fall ()
   {
     _body.iskinematic = false;
   }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions