Hello, my Instantiate repeatedly spawns because it is in Update. I am trying to see if i can get it to spawn once from the update method. but am unsure how to do so. Some help would be great or a point in the right direction.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mover : MonoBehaviour
{
Spawner spawn;
// Start is called before the first frame update
void Start()
{
spawn = FindObjectOfType<Spawner>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
transform.position += Vector3.down * 280 * Time.deltaTime;
}
if (GameObject.FindGameObjectWithTag("Trunk") == null)
{
spawn.SpawnTrunksR1();
}
}
}
i figured it out on my own HOOORRAAAAYYYY!!!.
I did it with delegates. like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Mover : MonoBehaviour
{
Spawner spawn;
// Start is called before the first frame update
void Start()
{
spawn = FindObjectOfType<Spawner>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
transform.position += Vector3.down * 280 * Time.deltaTime;
}
if (GameObject.FindGameObjectWithTag("Trunk") == null)
{
spawn.m_methodToCall();
}
}
}
[/code]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
//public GameObject trunk;
public float minHeight = 0.1f;
public float maxHeight = 1f;
public delegate void TestDelegate();
public TestDelegate m_methodToCall;
public bool trunk;
// Start is called before the first frame update
void Start()
{
SpawnTrunks();
m_methodToCall = SpawnTrunks;
}
public void SimpleMethod(TestDelegate SpawnTrunksR1)
{
Debug.Log(“I’m about to call a method”);
// method();
SpawnTrunksR1();
}
public void SpawnTrunks()
{
// Instantiates a Prefab named “enemy” located in any Resources
// folder in your project’s Assets folder.
GameObject instance = Instantiate(Resources.Load(“Tree1”, typeof(GameObject))) as GameObject;
}
public void SpawnTrunksR1()
{
// Instantiates a Prefab named “enemy” located in any Resources
// folder in your project’s Assets folder.
GameObject instance = Instantiate(Resources.Load(“Tree2”, typeof(GameObject))) as GameObject;
}
public void SpawnTrunksR2()
{
// Instantiates a Prefab named “enemy” located in any Resources
// folder in your project’s Assets folder.
GameObject instance = Instantiate(Resources.Load(“Tree3”, typeof(GameObject))) as GameObject;
}
// Update is called once per frame
void Update()
{
}
}
[/code]
For things you want to just do once, a bool is often the correct tool. At least I’d use a bool unless you have a reason not to. Also, evaluate the bool first in the “if” statement, since evaluating a bool is trivial while any of the “Find” methods are notoriously costly. The “if” statement evaluates left to right and stops evaluating once it already is clear it will be false.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mover : MonoBehaviour
{
Spawner spawn;
bool spawned = false;
// Start is called before the first frame update
void Start()
{
spawn = FindObjectOfType<Spawner>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
transform.position += Vector3.down * 280 * Time.deltaTime;
}
if ((spawned == false) && (GameObject.FindGameObjectWithTag("Trunk") == null))
{
spawn.SpawnTrunksR1();
spawned = true;
}
}
}
1 Like
Thanks Joe, Thats works perfectly i was using bools but not in the if statement i thought i tried every option. It looks so easy now. i Wish i had half of your brain power.
1 Like