Creating Modular Firing Mechanism
Hello at first. I've been developing a 2D Platformer game w/mouse aim and, I was trying to create a prefab based firing mechanism.
At my main player controller
public class PlayerController : MonoBehaviour {
...........
//Weapon related variables.
public Component WeaponController;
//At first.
void Start () {
WeaponController = transform.GetComponentInChildren<WeaponController>();
}
//Update
void Update (){
if(Input.GetKey(KeyCode.Mouse0)) {
WeaponController.Fire = true;
}
}
I'm trying to switch weapon controller's Fire bool to true (I have to access it using child, because it's going to be multiplayer game. Player dependent script.)
At weapon controller,
public class WeaponController : MonoBehaviour {
public static bool Fire = false;
// Update is called once per frame
void Update () {
if(Fire == true) {
Debug.Log ("Fire");
Fire = false;
}
}
}
I used a public static boolean to access, but Unity says
Assets/scripts/mech/PlayerController.cs(75,50): error CS1061: Type
UnityEngine.Component' does not contain a definition for
Fire' and no extension methodFire' of type
UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
What should I do for doing something modular ?
P.S: Newbie to C#, 17 years old Thanks.
Answer by Happy-Zomby · Apr 16, 2016 at 10:54 AM
Can you try this?
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
...........
//Weapon related variables.
public WeaponController weaponController;
//At first.
void Start()
{
weaponController = transform.GetComponentInChildren<WeaponController>();
}
//Update
void Update()
{
if (Input.GetKey(KeyCode.Mouse0))
{
weaponController.Fire = true;
}
}
I would also recommend renaming Fire to fire - normal name convention for variables is start with a small letter. hope that helps