- Home /
The question is answered, right answer was accepted
Avoid function being called every frame (on Start void)
Hi,
well I have a script that is attached to a gameobject the problem is that Start is called every frame (I don't know why) and there I have a function that adds some information to a Dictionary, and every second is created a new entry on the dictionary.
How can I call the function only one time?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Ammo {
public float ammoboxSize;
}
public class CurWeapons {
public static Dictionary<string, Ammo> ammoLib = new Dictionary<string, Ammo>();
public static void SetNewWeapon(string str, Ammo lib) {
Debug.Log ("Key " + str + " added.");
ammoLib.Add(str, lib);
}
public static Ammo RetrieveInfo(string name) {
return ammoLib[name];
}
public static void ChangeAmmo(string name, float bullets) {
Ammo tempAmmoPreset = ammoLib[name];
tempAmmoPreset.ammoboxSize = bullets;
ammoLib[name] = tempAmmoPreset;
}
}
public class AmmoScript : MonoBehaviour {
public float ammo_boxSize = 32;
// Use this for initialization
void Start () {
Ammo ammoPreset;
ammoPreset = new Ammo();
ammoPreset.ammoboxSize = ammo_boxSize;
CurWeapons.SetNewWeapon(transform.name, ammoPreset);
}
// Update is called once per frame
void Update () {
}
}
That code is used to determine the capacity of a weapon, and sets the number of bullets that It have.
Thanks. Bye.
What makes you think start is getting called every frame?
Place a Debug.Log() statement in your Start function, if it only returns once, then your Start function is working correctly.
I cant imagine a situation where Start would be called every second let alone every frame, unless you call it yourself at that frequency.
Can you provide any other information that might help us help you track down the issue your having.
Answer by z3nth10n · Feb 28, 2014 at 11:22 PM
I solve it, checking if the current key exists on the dictionary.
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
Enexpected Symbol Void (start) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers