I need help, Currently, I’m trying to make a system where I have 2 different bools and a float(baby, fem,fert) fert goes up every second and fem is a 50% chance on start and baby is true at the start. I have it set it up so that if Fert > 5 if fem = true, and if baby = true then it instantiates a new child and sets baby to false so it should only have one baby right? but the problem is it just spews out babies like so much that after 5 seconds my unity crashes I don’t know what’s causing this
Post your code ( use code tags ).
Remove “while( true )”
Don’t put the code in an Update() method, it will trigger many times a second.
Theoretically that’s fine as long as he has a flag he turns off after the first successful trigger, which he says he’s doing.
if (fert > 5)
{
if(fem == true)
{
if (baby == true)
{
Instantiate(mot);
baby = false;
}
}
}
the Instantiated object (mot) also has the same script btw
is mot a reference to an (inactive) prefab, or a reference to an already instantiated object (or to self)
if the latter, then the new mothers could already be fert >5
That’s not enough context. We need to know when that code is running (e.g. is it in Update?), and what “mot” is. We might also need to see the code that modifies the values of those variables that you’re checking.
It’s likely you should post all the code from that entire file (but it depends how much unrelated stuff is in the same file, and how much related stuff is in other files).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class foodSearch : MonoBehaviour
{
public float speed;
Vector3 force;
public float rot;
public float hunger;
public int stomach;
public int food;
public int Gend;
public float fert;
public bool baby;
public bool isBaby;
public GameObject mot;
public float timet;
public bool fem;
// Start is called before the first frame update
void Start()
{
Vector3 force = transform.forward;
rot = Random.Range(-360, 360);
transform.Rotate(Vector3.up * rot);
stomach = Random.Range(20, 60);
hunger = stomach - 1;
speed = Random.Range(0.1f, 0.05f);
Gend = Random.Range(0, 2);
baby = true;
if (Gend == 1)
{
fem = true;
}
if (Gend == 2)
{
fem = false;
}
}
// Update is called once per frame
void Update()
{
//rb.AddForce(transform.forward * Time.time * speed);
transform.Translate(Vector3.forward * speed);
hunger -= 1 * Time.deltaTime;
timet = Time.time * 1;
if (hunger < 0)
{
Destroy(gameObject);
}
if (baby == true)
{
fert = timet * 1;
}
if (fem == true)
{
if (fert > 5)
{
fert = 0;
baba();
baby = false;
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "apple")
{
rot = Random.Range(-360, 360);
transform.Rotate(Vector3.up * rot);
hunger = hunger + food;
Destroy(collision.gameObject);
}
if (collision.gameObject.tag == "stone")
{
rot = Random.Range(-360, 360);
transform.Translate(Vector3.back * Time.deltaTime);
transform.position = new Vector3(0, 1.13f, 0);
}
if (collision.gameObject.tag == "water")
{
Destroy(gameObject);
}
}
private void baba()
{
Instantiate(mot);
}
}
I’ll just code dump. This is my main script
Well, that’s quite a bit different from what you posted earlier! This one doesn’t check if baby is true before calling Instantiate.
Looks like you’re trying to prevent multi-spawning by resetting fert to 0. I don’t think fert is doing what you imagine, though; you’re only increasing it if baby is true, but when you DO increase it, you just set it equal to Time.time. So you’re not checking whether baby has been true FOR 5 seconds, you’re checking whether baby is true AND the game has been running for at least 5 seconds.
But that still doesn’t explain the infinite loop on its own, so there’s probably still an important piece that you haven’t posted. What sort of object do you actually have assigned to the “mot” variable? My best guess is that that object has a “foodSearch” component attached, which immediately runs Start, which sets baby to true, which causes it to immediately spawn a child because Time.time > 5. But it could also be something else changing the value of the baby variable, or some other interaction we can’t see in the code you posted.
Is there a specific reason for code like this? Multiplying by one doesn’t change a value.
fert = timet * 1;
cuz imma dumb
Thank you for your help! but ive just gone and made a new manager script which is separate from the dummies. Thank you!
Additional point:
Gend = Random.Range(0, 2);
if (Gend == 1)
{
fem = true;
}
if (Gend == 2)
{
fem = false;
}
Your random range returns either 0 or 1.
Its only working by chance, because fem defaults to false, the ‘2’ is never rolled
Some possibel improvements:
- You are only interested if the object is fem. A simpler approach would be to replace “int Gend” with “bool isFem”.
- No need to compare a bool to true. Instead of “if (fem == true)” simply write “if (fem)”. renaming bools to vaguely Sound like the question that their value answers helps a lot. That’s why I named it ‘isFem’.
- Directly nesting
if (fem) {
if (babe){
// do this
}
}
can ususally be much better expresssed as
if (fem && babe){
// do this
}
- Multiplying with constant. It’s not really necessary in C#m but if you are multiplying a float with a constant, it’s good practice to add the ‘f’ after the constant number to reming yourself you are dealing with a float, e.g.
time = time * 1f;