So I'm trying to make some buttons for a "shop window" in my game. I cannot see where I went wrong in the code, but I get this error:
Assets/shopHandler.cs(34,17): error CS1061: Type
`UnityEngine.GameObject' does not contain a definition for
`transfrom' and no extension method `transfrom' of type
`UnityEngine.GameObject' could be found. Are you missing an assembly
reference?
My code:
using System.Collections;
using UnityEngine;
public class shopHandler : MonoBehaviour {
[System.Serializable]
public class Item
{
public string name;
public Sprite icon;
public float price;
public float dps;
public int acquired;
}
public Item[] shopItems;
public GameObject button;
// Use this for initialization
void Start () {
foreach (Item i in shopItems)
{
GameObject btn = (GameObject)Instantiate(button);
ItemScript scp = btn.GetComponent<ItemScript>();
scp.name.text = i.name;
scp.price.text = "Price: $" + i.price.ToString("F1");
scp.acquired.text = i.acquired.ToString();
scp.dps.text = "$/s: " + i.dps.ToString("F1");
scp.icon.sprite = i.icon;
btn.transfrom.SetParent(this.transform);
}
}
// Update is called once per frame
void Update () {
}
}
Because its "transform", not "transfrom". A little typo in your last line inside your start method.
Related
Hey i am beginer and i am making a multiplayer game using mirror by watching https://www.youtube.com/watch?v=w0Dzb4axdcw&list=PLDI3FQoanpm1X-HQI-SVkPqJEgcRwtu7M&index=3 this video in this video he has maded match maker script and i have maded it step by step but don't know why i am getting this error i have seen code many times and all the things are same but he is not getting any error but i am plzz help this is my code and plzz explain in simply i am beginner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
namespace MirrorBasics {
[System.Serializable]
public class Match {
public string matchID;
public SyncListGameObject players = new SyncListGameObject ();
public Match(string matchID, GameObject player) {
this.matchID = matchID;
players.Add (player);
}
public Match () { }
}
[System.Serializable]
public class SyncListGameObject : SyncList<GameObject> { }
[System.Serializable]
public class SyncListMatch : SyncList<Match> { }
public class MatchMaker : NetworkBehaviour {
public static MatchMaker instance;
public SyncListMatch matches = new SyncListMatch ();
public SyncListString matchIDs = new SyncListString ();
void Start() {
instance = this;
}
public bool HostGame (string _matchID, GameObject _player) {
if (!matchIDs.Contains(_matchID)) {
matchIDs.Add (_matchID) ;
matches.Add (new Match (_matchID, _player));
Debug.Log ($"Match generated");
return true;
} else {
Debug.Log ($"Match ID already exists");
return false;
}
}
public static string GetRandomMatchID () {
string _id = string.Empty;
for (int i = 0; i < 5; i++) {
int random = Random.Range(0, 36);
if (random < 26) {
_id += (char)(random + 65);
} else {
_id += (random - 26).ToString ();
}
}
Debug.Log($"Random Match ID: {_id}");
return _id;
}
}
}
Like SyncListGameObject create SyncList with string like this.
public class SyncListString: SyncList<string>()
Then
public SyncListString matchIDs = new SyncListString();
I made lobby/matchmaking by this tutorial too :)
(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);
}
}
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
These two blocks of code I assume are involved with the following error:
Assets\Scripts\Weapons.cs(8,12): error CS7036: There is no argument given that corresponds to the required formal parameter 'name' of 'Item.Item(string, int)'
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Item : MonoBehaviour
{
private string name;
private int quantity;
public Item(string name, int quantity)
{
this.name = name;
this.quantity = quantity;
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Quantity
{
get { return quantity; }
set { quantity = value; }
}
public virtual void UseItem()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapons : Item
{
public GameObject bullet;
public GameObject shotgunSpawn;
public GameObject shotgunSpawn2;
//public bool useGun;
public Inventory iw;
GUI_2D m;
public float Speed;
GameObject patroller;
GameObject guard;
public bool pellet;
public bool shotGun;
public bool barGun;
public PlayerController player;
// Start is called before the first frame update
void Start()
{
Speed = 5f;
player = GameObject.FindGameObjectWithTag("Player");
patroller = GameObject.FindGameObjectWithTag("Patroller");
guard = GameObject.FindGameObjectWithTag("Guard");
guard = GameObject.FindGameObjectWithTag("Shotgun");
pellet = false;
shotGun = false;
barGun = false;
}
void DestroyEnemy()
{
if (patroller)
{
patroller.SetActive(false);
}
if (guard)
{
patroller.SetActive(false);
}
}
private void OnTriggerEnter(Collider other)
{
if (iw.invItems.Count < 12)
{
if (other.gameObject.CompareTag("Player"))
{
pellet = true;
}
}
}
public override void UseItem()
{
if (pellet)
{
player.pellet = true;
player.shotGun = false;
player.barGun = false;
}
if (shotGun)
{
player.pellet = false;
player.shotGun = true;
player.barGun = false;
}
if (barGun)
{
player.pellet = false;
player.shotGun = false;
player.barGun = true;
}
base.UseItem();
}
}
I am not trying to change the item class since doing so will affect another class that depends on the item classes constructor. Unless there is another way to resolve the error by changing the item class. The error given is also affecting another class in the same way. I am hoping to solve the issue in both the weapon class and the other class from an answer here. Thank you in advance.
You shouldn't add constructors to MonoBehaviours, as the Unity Engine is responsible for instantiating them and will never pass your constructors arguments (you shouldn't be creating instances of MonoBehaviours, either).
Just remove your Item constructor and assign the values manually whenever needed. Other construction code should be done in Awake() or Start().
I have an issue with increasing the score in my space invaders clone. I have 2 errors.
Assets/Scripts/ScoreManager.cs(26,12): error CS0103: The name
`retryLevel' does not exist in the current context
Assets/Scripts/ScoreManager.cs(55,43): error CS1061: Type int' does
not contain a definition forTostring' and no extension method
Tostring' of typeint' could be found. Are you missing an assembly
reference?
Where and what exactly in my script should I change?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour
{
int i = 0;
public GameObject HighScoreDisplay;
public Text scoreDisplay;
public int score = 0;
public Text[] highScoreTables;
private void Start()
{
if (HighScoreDisplay == null || scoreDisplay == null) {
Debug.LogWarning("Values are missing on the ScoreManager!");
return;
}
retryLevel();
}
private void update()
{
scoreDisplay.text = score.ToString();
}
public void ModifyScore(int scoreToAdd)
{
score += scoreToAdd;
}
public void fromScratch(){
score = 0;
HighScoreDisplay.SetActive(false);
}
public void PlayerDied()
{
HighScores.AddScore(score);
foreach (Text table in highScoreTables)
{
table.text = HighScores.scoreTable[i].Tostring();
i++;
}
HighScoreDisplay.SetActive(true);
score = 0;
}
}
public static class HighScores
{
public static List<int> scoreTable = new List<int>{0,0,0};
public static void AddScore(int score)
{
if (score > scoreTable[2])
{
scoreTable[2] = score;
}
else if (score > scoreTable[1])
{
scoreTable[1] = score;
}
else if (score > scoreTable[0])
{
scoreTable[0] = score;
}
}
}
These errors are simple:
your code does not contain definition for function retryLevel nor do I see any static using to be available for importing such a function from other class, (It is a inherited function from MonoBehaviour? Are you sure it has not uppercase first letter according to C# standards?)
you have Tostring with small S. It should be ToString.
Please help me fix the error.
The message is:
"Assets/Scripts/GameManager.cs(6,21): error CS0118: GameManager.character' is afield' but a `type' was expected"
The error is on (6, 21). Thanks.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameManager : MonoBehaviour {
public List<character> character = new List<character>(); <-- (ERROR CS0118)
bool ShowCharWheel;
public int SelectedCharacter;
public int xcount;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.C)){
{
ShowCharWheel = true;
}
{
ShowCharWheel = false;
}
//Camera.main.GetComponent<SmoothFollow>().target = characters[SelectedCharacter].
if (ShowCharWheel)
{
GUILayout.BeginArea(new Rect(Screen.width - 64, Screen.height - 192,64,192));
{
if(GUILayout.Button(c.icon,GUILayout.Width(64),GUILayout.Height(64)))
foreach (character c in Characters)
{
SelectedCharacter = character.IndexOf(c);
}
}
}
GUILayout.EndArea();
}
}
}
[System.Serializable]
public class Character
{
public string name;
public Texture2D icon;
public GameObject prefab;
public GameObject instance;
public Transform HomeSpawn;
}
Needed more details other than code, so ignore this.
Do you perhaps just need to capitalize? C# is case sensitive.
public List<Character> characterList = new List<Character>();
I also renamed your variable for clarity.