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.
Related
I am using UnityEvent I called ScoreEvent and I feed it a float. All works fine, except when I need it to. I tried invoking the event when an enemy dies. Nada. I tried putting it in an If statement before, and for some reason it works.
This is the main code (ignore useless variables, still seeing what I need and what I don't)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
[RequireComponent(typeof(TriggerEnter))]
public class Health : MonoBehaviour
{
private ScriptableObjectLoader _sol;
private TriggerEnter _te;
public HealthbarEvent HE;
public ScoreEvent SE;
public float ObjectHealth;
public float EnemyScore;
private float _score;
private bool _isDead;
private void Awake() {
if(_te == null)
{
_te = GetComponent<TriggerEnter>();
}
if(HE == null)
{
HE = new HealthbarEvent();
}
if(SE == null)
{
SE = new ScoreEvent();
}
}
void Start()
{
this._sol = GetComponent<ScriptableObjectLoader>();
_te.DE.AddListener(onChange);
this.ObjectHealth = _sol.Health;
this._score = _sol.Score;
this._isDead = false;
HE.Invoke(ObjectHealth);
}
void onChange(Damage damage){
ObjectHealth -= damage.damage;
if(gameObject.tag == "Player")
{
HE.Invoke(ObjectHealth);
//Works if i put SE.Invoke here
}
if(ObjectHealth <= 0)
{
SE.Invoke(EnemyScore); //doesn't work here (I WANT IT TO BE HERE, NOT UP THERE) :/
if(gameObject.tag == "Enemy")
{
EnemyScore = gameObject.GetComponent<Health>()._score;
}
Destroy(gameObject);
}
}
}
[System.Serializable]
public class ScoreEvent : UnityEvent<float>{}
public class HealthbarEvent : UnityEvent<float>{}
And this is the script that listenes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ScoreLoader : MonoBehaviour
{
private TMP_Text _tekst;
private GameObject _player;
private Health _h;
void Awake() {
_tekst = GetComponent<TMP_Text>();
}
void Start()
{
_player = GameObject.Find("Player");
_h = _player.GetComponent<Health>();
_h.SE.AddListener(updateScore);
_tekst.text = "0";
}
void Update()
{
}
void updateScore(float score)
{
Debug.Log("Primio sam ga. Jako");
}
}
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 :)
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 three characters and each of them has a camera attached to them.By default they are disabled but one.I made a character selector which is supposed to change them.I have a problem where I can move the selected one but the camera stays at the last one.
Here is the script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityStandardAssets.Utility;
public class GameManageer : MonoBehaviour {
public Camera[] cams = new Camera[3];
public Character CurrentCharacter;
public List<Character> Characters = new List<Character>();
public List<Item> AllItems;
bool ShowCharWheel;
public int SelectedCharacter;
public int lastCharacter;
public static GameManageer Instance;
void Awake(){
Instance = this;
foreach (Character c in Characters){
c.Instance = Instantiate(c.PlayerPrefab, c.HomeSpawn.position, c.HomeSpawn.rotation) as GameObject;
c.Instance.GetComponent<PlayerController>().LocalCharacter = c;
}
ChangeCharacter(Characters[PlayerPrefs.GetInt("SelectedChar")]);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.C)) {
ShowCharWheel = true;
} else {
ShowCharWheel = false;
}
}
void ChangeCharacter(Character c){
lastCharacter = SelectedCharacter;
SelectedCharacter = Characters.IndexOf (c);
cams [SelectedCharacter].gameObject.SetActive (true);
cams [lastCharacter].gameObject.SetActive (false);
CurrentCharacter = c;
Characters [lastCharacter].Instance.GetComponent<PlayerController> ().CanPlay = false;
Characters [SelectedCharacter].Instance.GetComponent<PlayerController> ().CanPlay = true;
PlayerPrefs.SetInt ("SelectedChar", SelectedCharacter);
}
void OnGUI(){
if (ShowCharWheel) {
GUILayout.BeginArea(new Rect(Screen.width - 64, Screen.height - 192,64,192));
foreach (Character c in Characters){
if (GUILayout.Button(c.Icon,GUILayout.Width(64),GUILayout.Height(64))){
ChangeCharacter(c);
}
}
GUILayout.EndArea();
}
}
}
[System.Serializable]
public class Character {
public string Name;
public Texture2D Icon;
public GameObject PlayerPrefab;
public GameObject Instance;
public Transform HomeSpawn;
}
[System.Serializable]
public class Item{
public string Name;
public Texture2D Icon;
public ItemInstance InstancePrefab;
}
This should do the job:
cams[SelectedCharacter].enabled = true;
cams[lastCharacter].enabled = false;
Use Depth:
foreach (Camera cam in cams)
{
cam.depth = cam == cams[SelectedCharacter] ? 10 : 0;
}
I think the real problem here though, is that you have more cameras in the scene which you have to manage as well, other then only the last and current selected character... in which case:
foreach (Camera cam in cams)
{
cam.SetActive(cam == cams[SelectedCharacter]);
}
using UnityEngine;
using System.Collections;
public class Weapon : MonoBehaviour {
public string Name;
public int RateOfFire;
int ROF;
public int Accuracy;
public int Ammo;
public Bullet Amunition;
public PCP shootingPoint;
[HideInInspector]
public bool IsActive = false;
void Start ()
{
ROF = 0;
}
// Update is called once per frame
public void WeaponUpdate ()
{
if(ROF != 0)
{
ROF --;
}
}
public void Shoot()
{
if(Ammo > 0 && ROF == 0)
{
shootingPoint.SendMessage("Create",Amunition);
Ammo --;
ROF = RateOfFire;
}
}
}
"Note : PCP is shortcut to Prefab Shooting Point".
I got this error in the shooting method in the line "shootingPoint.SendMessage" I just dont understand why ? and I have the Components at the objects I placed in the shootingPoint and the Ammunation , so what is wrong ??
Here is an image to prove I attached objects :
Either shootingPoint or Amunition aren't objects yet. They're the only two things on that line that could be causing an issue.