Instantiate only once? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How do Instantiate only once? This script continuously create clones.
GameObject[] cubeParticules;
void OnTriggerEnter()
{
foreach(GameObject part in cubeParticules)
{
Instantiate(part, temp1, Quaternion.identity);
}
}
No scripts attached to cubeParticules

The foreach term (if it wasn't obvious enough) performs the contained code for each object that is true to the condition; in this case: for each game-object contained in the cubeParticles array.
After your edit:
Also, your colliders may be touching each other at multiple entry points.
Try using a bool flag that is set once the objects are initiated, and then reset in OnTriggerExit().
Here's some sample code:
void OnTriggerEnter()
{
...
if (!instantiated)
{
foreach(GameObject part in cubeParticules)
{
Instantiate(part, temp1, Quaternion.identity);
}
...
instantiated = true;
}
...
}
void OnTriggerExit()
{
...
instantiated = false;
...
}
I hope that helps!

Related

I am making a "boss fight" in unity2d. I wanted my player to get Iframes on hit. And it works but it bugs out so quickly. Any idea why it gets bugged? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
So basically i did a character with 3 health. It seemed like this character needed iframes because he was getting stuck to death very quickly. So i made it so when he gets hit he is untouchable for 5 seconds and it worked but was bugging out too frequently (in about every 8-9 hits).
What i mean by bugging out is... My character did not get hit at all, infinitely. until my character died or i restarted the game.
Here is my code
If you need any of the other codes just say it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Iframes : MonoBehaviour
{
Renderer rend;
Color c;
GameObject character;
// Start is called before the first frame update
void Start()
{
rend = GetComponent<Renderer> ();
c = rend.material.color;
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
StartCoroutine("GetInvulnerable");
}
}
IEnumerator GetInvulnerable()
{
Physics2D.IgnoreLayerCollision(8, 9, true);
yield return new WaitForSeconds(3f);
Physics2D.IgnoreLayerCollision(8, 9, false);
}
}

How can i make a player stop moving when it touchs a collider? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
So i was wondering if there is a way to make a player move infinitely with just one click and just stop when colliding with another collider.
You can create a global property:
bool movePlayer = false;
In FixedUpdate method you can detect a mouse click and change movePlayer to true, and move the player if it values true:
void FixedUpdate() {
if (Input.GetMouseButtonDown(0))
movePlayer = true;
if (movePlayer) {
//move the player here, maybe with Translate method
}
}
With OnTriggerEnter method you can detect the collision and change movePlayer to false:
void OnTriggerEnter(Collider other) {
//you can check for some particular object or avoid this if
if (other.gameObject.name == "SomeObject")
movePlayer = false;
}

please help me with my game my game have a problem [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am making a objectpoolingManager which is supposed to put bullets under an empty object called objectpoolingManager and in my code i wrote that it must enable the bullet when i click the mouse button then after 2 second it will diables the bullet now my problem is that it is always disabled.
public GameObject bulletPrefab;
public int bulletAmount = 20;
private List<GameObject> bullets;
// Start is called before the first frame update
void Awake()
{
instance = this;
bullets = new List<GameObject>(bulletAmount);
for (int i = 0; i < bulletAmount;i++)
{
GameObject prefabInstance = Instantiate (bulletPrefab);
prefabInstance.transform.SetParent (transform);
prefabInstance.SetActive(false);
}
bullets.Add(bulletPrefab);
}
public GameObject GetBullet ()
{
foreach (GameObject bullet in bullets)
{
if(!bullet.activeInHierarchy)
{
bullet.SetActive (true);
return bullet;
}
}
GameObject prefabInstance = Instantiate (bulletPrefab);
prefabInstance.transform.SetParent (transform);
bullets.Add(bulletPrefab);
return prefabInstance;
}
}
Your getter/setter of objects in your pool manager should not instantiate or destroy gamobjects, but enable and disable them. That is the purpose of pooling.
It makes more sense to EnQueue/deQueue them and activate/deactivate like this.
private Queue<GameObject> objPool;
private Queue<GameObject> activeObj;
//get from pool
public GameObject GetObjFromPool(Vector3 newPosition, Quaternion newRotation)
{
GameObject newObject = objPool.Dequeue();
newObject.SetActive(true);
newObject.transform.SetPositionAndRotation(newPosition, newRotation);
//keep actives to be retrieved
activeObj.Enqueue(newObject);
return newObject;
}
//return to pool
public void ReturnObjToPool(GameObject go)
{
go.SetActive(false);
objPool.Enqueue(go);
}
Find this question in case its helpfull.
You can find plenty of pooling examples if you goolgle that up. Here is one.

How to turn off all the sounds in unity on click of button [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
public void Click(){
changeState();
}
private void changeState(){
isClicked = !isClicked;
if(isClicked) myBtn.image.sprite = Play;
else myBtn.image.sprite = Pause;
}
I have attached the above code to change the button image.
and now how can I turn off all the sounds.
Any help would be great.Thank you.
You can turn of all sound in Unity by pausing the AudioListener.
AudioListener.pause = true;
If you want it to toggle each time the button is clicked, you could use:
AudioListener.pause = !AudioListener.pause;
Finally, if all you want to do is to reduce the volume of all sound in your game:
float yourVolume = 1f;
AudioListener.volume = yourVolume;
In addition to the suggestion in first answer, you can also disable the
audio-listener in your scene. Or, if you can access all the audio-sources
you can stop each one of them.
public AudioListener audioListener;
public List<AudioSource> audioSources;
void OnMouseDown() {
foreach(AudioSource audioSorce in audioSources) {
audioSorce.Stop();
}
/*
if(audioListener != null) {
audioListener.enabled = false;
}
*/
}

Unity 3D spawn 10 objects on mouseclick [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
void Update ()
{
if (Input.GetMouseButtonDown ("Fire1"))
{
}
}
How do I spawn my prefab on click?
To instantiate a prefab you can use Instantiate (as someone told you in a comment) https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
to do that 10 times use a simple for-loop: for(int i=0; i<10; ++i){ //code }
so, putting all togheter the update functions can be:
void Update ()
{
if (Input.GetMouseButtonDown ("Fire1"))
{
for (int i = 0; i < 10; ++i){
Instantiate(m_oMyPrefab, m_oMyPosition, m_oMyRotation);
}
}
}
Note that m_oMyPrefab must be a GameObject variable with a reference to your prefab (you can do it programmatically or with the inspector editor), m_oMyPosition must be a Vector3 with the desired position and m_oMyRotation must be a Quaternion. Position and rotation are optionals, see the documentation for more details.

Categories