Hi Guys,
I need help with the following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cloud : MonoBehaviour
{
public GameObject cloud;
public float speed;
public int destroycloud;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Createcloud();
cloud.transform.position += transform.forward * speed * Time.deltaTime;
}
void Createcloud()
{
if (GameObject.Find(“clouds”) == null)
{
cloud = Instantiate(cloud, transform.position, Quaternion.Euler(-90, 0, 0)) as GameObject;
}
}
}
I want my empty gameobject to instantiate the cloud prefab if there is none spawned in. When i run this code however, the cloud prefab is being generated continuously and it’s like the condition is being ignored.
here is a screenshot of my unity setup. Any help is appreciated!
When posting code to the forum, make sure you use Code Tags so things are easier to read.
You can see in the Hierarchy that your new clouds are no longer called “cloud”, they are called “cloud(Clone)”, “cloud(Clone)(Clone)” etc.
If you only want one cloud, why not put the cloud creation in Start()?
Thanks for your message! I’ll do that in the future.
I’m still learning unity and coding and I’m working my way to generating clouds. Right now, i want to generate 1 cloud with 1 empty game object and have it move across the screen for x amount of time. After that, the cloud should destroy itself and the script should see that there’s no clouds in the scene and generate a new one.
Am i approaching this the wrong way? I’d like to keep my code as simple as possible for now
Is there anything wrong with just moving the cloud to the initial position after the amount of time? It would essentially look the same as “destroying the cloud and making a new one”, but would be more efficient. You can keep track of how much time has passed and reset it when you reach that threshold.
:0 That’s Genius!
Would you mind pointing me in the right direction? I’ve never worked with time in code besides understanding the update and start functions. How would i keep track of how long the object has been moving for? would it be easier to see track how far the object has moved and once it hits a threshold, move it back to the initial position?
Sorry for all the questions and i really appreciate your feedback!
Nevermind, i got it working by calculating the distance the cloud has moved and resetting it back to the position of the empty gameobject.
posted my code if anyone wants to see it. Thanks again!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cloud : MonoBehaviour
{
public GameObject cloud;
public float speed;
float distancetraveled;
Vector3 lastposition;
// Start is called before the first frame update
void Start()
{
cloud = Instantiate(cloud, transform.position, Quaternion.Euler(-90, 0, 0)) as GameObject;
lastposition = transform.position;
}
// Update is called once per frame
void Update()
{
cloud.transform.position += transform.forward * speed * Time.deltaTime;
distancetraveled = Vector3.Distance(this.transform.position, lastposition);
lastposition = cloud.transform.position;
if (distancetraveled > 10f)
{
cloud.transform.position = this.transform.position;
}
}
}
1 Like