C# 调用游戏对象数组';脚本名为';苹果';来自一个名为';货币';

C# 调用游戏对象数组';脚本名为';苹果';来自一个名为';货币';,c#,arrays,unity3d,C#,Arrays,Unity3d,(在unity 2D中)因此我有一个脚本,用于计算游戏中的金钱和苹果数量,并希望使多个游戏对象具有苹果数量脚本。顺便说一句,这是一个空白的钱,是公开的。我这样做了,我需要将我的钱转移到apple脚本中,但因为它是一个数组,所以它会出现以下错误:(55,16):错误CS1061:“apple[]”不包含“TransferMoney”的定义,并且找不到可访问的扩展方法“TransferMoney”接受类型为“apple[]”的第一个参数(您是否缺少using指令或程序集引用?) public cl

(在unity 2D中)因此我有一个脚本,用于计算游戏中的金钱和苹果数量,并希望使多个游戏对象具有苹果数量脚本。顺便说一句,这是一个空白的钱,是公开的。我这样做了,我需要将我的钱转移到apple脚本中,但因为它是一个数组,所以它会出现以下错误:(55,16):错误CS1061:“apple[]”不包含“TransferMoney”的定义,并且找不到可访问的扩展方法“TransferMoney”接受类型为“apple[]”的第一个参数(您是否缺少using指令或程序集引用?)


public class numberofmoney : MonoBehaviour //script in the Text UI "amount of money" {

 static public int scenemoney;
 public string house;
 public string shopString;

 public TMP_Text moneyText;
 public cookie1 Cookie;
 public apple[] apples;
 public void BoughtApple(int currentAOM)//aom stands for 'amount of money'
 {
     scenemoney = currentAOM;
 }
 void Awake()
 {
     apples = GameObject.FindObjectsOfType<apple>();  //finds apple
 }
 void Start()
 {
     Scene cookie = SceneManager.GetActiveScene();
     house = cookie.name;    //checks scene and does part of converting to string
     Scene shop = SceneManager.GetActiveScene();
     shopString = shop.name; //checks scene and does part of converting to string

 }
 public void forCookie(int money)
 {
     scenemoney = money;
 }
 void Update()
 {
     string scenemoneystring = scenemoney.ToString();
     moneyText.SetText(scenemoneystring); //Converts money and sets text
     if (house == "House") {     //transfers money between scripts #1
     Cookie.transferMoney(scenemoney);
     }
     if (shopString == "store") { //transfers money between scripts #2
     apples.TransferMoney(scenemoney);
     }
 }

} 

公共类numberofmoney:MonoBehavior//文本UI中的脚本“金额”{
静态公共场景货币;
公共弦乐馆;
公共字符串商店字符串;
公共TMP_文本;
公共烹饪1曲奇;
公众苹果[]苹果;
public void BoughtApple(int currentAOM)//aom代表“金额”
{
scenemoney=当前AOM;
}
无效唤醒()
{
apples=GameObject.FindObjectsOfType();//查找苹果
}
void Start()
{
场景cookie=SceneManager.GetActiveScene();
house=cookie.name;//检查场景并执行部分转换为字符串
场景商店=SceneManager.GetActiveScene();
shopString=shop.name;//检查场景并执行转换为字符串的部分操作
}
公开作废forCookie(整数货币)
{
情景货币=金钱;
}
无效更新()
{
字符串scenemoneystring=scenemoney.ToString();
moneyText.SetText(scenemoneystring);//转换货币和设置文本
如果(house==“house”){//在脚本之间转账#1
Cookie.transferMoney(场景货币);
}
如果(shopString==“store”){//在脚本之间转账#2
苹果。转让货币(场景货币);
}
}
} 
和苹果剧本:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro;

public class apple : MonoBehaviour {

 public string appleString;
 public int publicMoney;
 public int Apples;
 public NumberOfApples numberOfApples;
 public TMP_Text tmp;
 public numberofmoney NumberOfMoney;
 void Awake()
 {
     numberOfApples = GameObject.FindObjectOfType<NumberOfApples>();
     NumberOfMoney = GameObject.FindObjectOfType<numberofmoney>();
 }
 public void TransferMoney(int money) 
 {
     publicMoney = money;
 }
 void OnTriggerEnter2D(Collider2D trigger)
 {
     if (publicMoney >= 10){
         Destroy(this.gameObject);
         Apples++;
         publicMoney -= 10;
         appleString = Apples.ToString();
         tmp.SetText(appleString);
         NumberOfMoney.BoughtApple(publicMoney);
     }
     numberOfApples.transferApples(Apples);
 }
} 
使用System.Collections;使用System.Collections.Generic;使用UnityEngine;使用UnityEngine.UI;使用TMPro;
公共类苹果:单一行为{
公共字符串appleString;
公共资金;
公众参与;
公众人数苹果人数苹果;
公共TMP_文本TMP;
公众号码货币号码货币;
无效唤醒()
{
NumberOfApple=GameObject.FindObjectOfType();
NumberOfMoney=GameObject.FindObjectOfType();
}
公共货币(整数货币)
{
公共货币=货币;
}
无效OnTriggerEnter2D(碰撞R2D触发器)
{
如果(公共资金>=10){
摧毁(这个游戏对象);
苹果++;
公帑-=10;
appleString=Apples.ToString();
tmp.SetText(appleString);
NumberOfMoney.BoughtApple(公共货币);
}
苹果数。转移苹果(苹果);
}
} 

据我所知,您正在做:

apples.TransferMoney(scenemoney);
但是apple是类apple的一个数组,您不能调用“transferMoney”方法,您需要迭代数组的每个对象并单独调用transferMoney方法

foreach(apple a  in apples){
    a.TransferMoney(scenemoney);
}
希望有帮助


编辑:derHugo调用的复制粘贴错误

您再次输入错误;)应该是
apple.TransferMoney
;)这成功了,谢谢!我必须查看foreach循环的功能,我想你每天都会学到新的东西!太棒了,你可以使用任何类型的循环,我使用foreach只是因为。你必须迭代数组(
for
foreach