- Home /
The question is answered, right answer was accepted
How to clone randomly
I have a 3d game where you drag things off the world but I need a cloner that clones my object randomly from x-2 to x2 and goes faster eg: every ten seconds speed increases by original speed *1.5. The other posts did not work for me. I tried nothing but the other posts. Thanks for helping!
Answer by xxmariofer · Jul 28, 2019 at 07:35 PM
public GameObject prefab;
public float x;//i dont know what x is
void Start() { StartCoroutine(Cloner()); }
float frequence = 10;
void Update() {
if(timer > 10) {
timer = 0; frequence = frequence / 1.5f;
}
timer += Time.deltaTime;
}
IEnumerator Cloner()
{
while(true){
yield return new WaitForSeconds(frequence);
Insntiate(prefab, new Vector3(x - 2, x + 2), Quaternion.identity);
}
}
Weird I got
Assets\Clone.cs(8,19): error CS1003: Syntax error, ',' expected
i missed a ; in the Instantiate line, i code it here without inteligence
Yeah i fixed that but now it says somthing else
Now it just does not work, when I play the game nothing else happens ._.
Thanks for the help so far I will give you 2 reputation points.
nothing happens? is the coroutine not getting triggered? add in update a Debug.Log(timer); and see if its updating correctly, and add inside the ienumerator next to the instantiate a Debug.Log("spawning"); if the spawning is getting printed the object is spawning but maybe far away or something depending on the x
Oh sorry, it works but it spawns where my first guy was, and because you throw the guy off the world the other clone spawns there too!
totally my fault change the instantiate line to this
Instantiate(prefab, new Vector3(Random.Range(x + 2, x - 2), 0, 0), Quaternion.identity)
Also, most people would quit right now so thanks for the help again take 3 reputations.
You are welcome :) $$anonymous$$ark It as correct for futuro readers
Do you know how to make a "landing base" or an area just for my clones ins$$anonymous$$d? Like a spawn point.
The troops get indtantiate in the terrain so creating a smaler terrain quad or box give the smaller terrain a new layer and make the ratcast only hit that layer
Also, my box collider leaves the player now do you know how to glue the box collider on?
if its leaves the player is because the physics is not getting updated, since the collider is "glued" by default, did you edit the fixed time value? or are yuo setting time.timescale to a value lower than 1?
Answer by zacharif · Aug 05, 2019 at 05:38 PM
Ok, I debugged it and it said it worked all the time! But when I dug deeper it still clones after one was thrown off but it disappears! When I clicked on it it said all the stuff it should have but it was gone except for the box collider? Any ideas?
Oh, It just does not work with ragdolls, only a $$anonymous$$or set back
Answer by zacharif · Aug 08, 2019 at 06:56 PM
FINAL CODE:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Clone : MonoBehaviour
{
public GameObject prefab;
public float x=2;//i dont know what x is
void Start() { StartCoroutine(Cloner()); }
float frequence = 10;
float timer = 0;
void Update()
{
if (timer > 10)
{
timer = 0; frequence = frequence / 0.5f;
}
timer += Time.deltaTime;
}
IEnumerator Cloner()
{
while (true)
{
yield return new WaitForSeconds(frequence);
Instantiate(prefab, new Vector3(Random.Range(0, 1), 0, Random.Range(0, 1)), Quaternion.identity);
Debug.Log("Work!");
}
}
}
after more than 20 comments you just copied my very first comment, and post it as an answer, and accepted your own answer? "FINAL CODE" i mean you are even using the exact same vars names, didnt you even test my code? thats really rude.
What? No! because your old code hade typos so I fixed those typos otherwise it would not work! If u want to have the answer than go ahead!
that you fixed the typos? is this a joke? the typo was that one ; was missing, and you didnt even fix it it was the one who told you that was missing, you are the most unpolite guy i have ever met in unity answers. you literally copied my code, there is not aa single difference. it took you more than 30 comments to even realize my code was working from the start is unbelivable that you believe you can waste our time like this
Follow this Question
Related Questions
Create Clones Randomly 1 Answer
Index out of range for no reason? 1 Answer
Creating a class? 2 Answers
Spawn random obiect 3d 1 Answer
Randomly choosing 5 gameobjects? 1 Answer