calling an array of gameobjects' script called 'apple' from a script called 'money'

Viewed 104

(In unity 2D)So I have a script for the amount of money and apples I have in my game and want to make more than 1 game object have the amount of apples script. btw there IS a void TransferMoney and IS public. I do this, and I need to transfer the amount of money i have to the apple script but because it's an array it does the following error: (55,16): error CS1061: 'apple[]' does not contain a definition for 'TransferMoney' and no accessible extension method 'TransferMoney' accepting a first argument of type 'apple[]' could be found (are you missing a using directive or an assembly reference?) Here's the money script:


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);
     }
 }

} 

and apples script:

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);
 }
} 
1 Answers

For what I can see, you're doing:

apples.TransferMoney(scenemoney);

but apples is an array of the class apple, you can't call the "transferMoney" method, you need to iterate each object of the array and call the transferMoney method individually.

foreach(apple a  in apples){
    a.TransferMoney(scenemoney);
}

hope that helps!

Edited: copy paste error called by derHugo

Related