using UnityEngine; public class ofertaMercado : M...

Creado el: 2 de marzo de 2025

Respondido usando O1 por Chat01

Pregunta

using UnityEngine;

public class ofertaMercado : MonoBehaviour
{
public recursos scriptRecursos;

text
public float[] ofertaMP = new float[9]; // producción base de los NPCs public float[] prodBaseNPC1MP = new float[9] { 12, 3, 1, 0, 0, 0, 0, 0, 0 }; public float[] prodBaseNPC2MP = new float[9] { 6, 9, 2, 0, 0, 0, 0, 0, 0 }; public float[] prodBaseNPC3MP = new float[9] { 18, 6, 3, 0, 0, 0, 0, 0, 0 }; // variación en la produccón public float[] varProdNPC1MP = new float[9] { 12, 3, 1, 0, 0, 0, 0, 0, 0 }; public float[] varProdNPC2MP = new float[9] { 6, 9, 2, 0, 0, 0, 0, 0, 0 }; public float[] varProdNPC3MP = new float[9] { 18, 6, 3, 0, 0, 0, 0, 0, 0 }; // producción total de los NPCs public float[] produccionNPC1MP = new float[9]; public float[] produccionNPC2MP = new float[9]; public float[] produccionNPC3MP = new float[9]; public void CalcularOferta() { for (int i = 0; i < 9; i++) { float delta1 = Random.Range(-varProdNPC1MP[i], varProdNPC1MP[i]); produccionNPC1MP = prodBaseNPC1MP + delta1; if (produccionNPC1MP[i] < 0) produccionNPC1MP[i] = 0; float delta2 = Random.Range(-varProdNPC2MP[i], varProdNPC2MP[i]); produccionNPC2MP = prodBaseNPC2MP + delta2; if (produccionNPC2MP[i] < 0) produccionNPC2MP[i] = 0; float delta3 = Random.Range(-varProdNPC3MP[i], varProdNPC3MP[i]); produccionNPC3MP = prodBaseNPC3MP + delta3; if (produccionNPC3MP[i] < 0) produccionNPC3MP[i] = 0; } int[] prodJugadorMP = new int[] { scriptRecursos.mP1, scriptRecursos.mP2, scriptRecursos.mP3, scriptRecursos.mP4, scriptRecursos.mP5, scriptRecursos.mP6, scriptRecursos.mP7, scriptRecursos.mP8, scriptRecursos.mP9 }; for (int i = 0; i < 9; i++) { ofertaMP[i] = produccionNPC1MP[i] + produccionNPC2MP[i] + produccionNPC3MP[i] + prodJugadorMP[i]; } Debug.Log(">>>OFERTA RECALCULADA. " + "\nGrano = " + ofertaMP[0] + "\nHortalizas = " + ofertaMP[1] + "\nFruta = " + ofertaMP[2]); }

}


Assets\Scripts\inicio1\sistemaEconomico\mercado\ofertaMercado.cs(29,32): error CS0019: Operator '+' cannot be applied to operands of type 'float[]' and 'float'
Assets\Scripts\inicio1\sistemaEconomico\mercado\ofertaMercado.cs(33,32): error CS0019: Operator '+' cannot be applied to operands of type 'float[]' and 'float
Assets\Scripts\inicio1\sistemaEconomico\mercado\ofertaMercado.cs(37,32): error CS0019: Operator '+' cannot be applied to operands of type 'float[]' and 'float'

Pensamiento

Adjusting the code structure

I’m addressing the type mismatch problem by updating the iteration logic and ensuring each expression uses the correct index variables. This modification should resolve the compilation error.

Reasoned for a few seconds

Respuesta

The issue arises because you’re attempting to add a float array (e.g., prodBaseNPC1MP) to a single float (delta1). Instead, you want to add the random delta per element of the array.

Inside your loop, do this instead:

csharp
for (int i = 0; i < 9; i++) { // Random delta for NPC1 float delta1 = Random.Range(-varProdNPC1MP[i], varProdNPC1MP[i]); produccionNPC1MP[i] = prodBaseNPC1MP[i] + delta1; if (produccionNPC1MP[i] < 0) produccionNPC1MP[i] = 0; // Random delta for NPC2 float delta2 = Random.Range(-varProdNPC2MP[i], varProdNPC2MP[i]); produccionNPC2MP[i] = prodBaseNPC2MP[i] + delta2; if (produccionNPC2MP[i] < 0) produccionNPC2MP[i] = 0; // Random delta for NPC3 float delta3 = Random.Range(-varProdNPC3MP[i], varProdNPC3MP[i]); produccionNPC3MP[i] = prodBaseNPC3MP[i] + delta3; if (produccionNPC3MP[i] < 0) produccionNPC3MP[i] = 0; }

This way, you add the random delta to each element of the production array, rather than trying to add a float array and a single float (which is invalid).

Compartir este Q&A