Question by
Bigbluedrag0n · Feb 26, 2021 at 09:53 PM ·
c#2dscripting problembegginer
How do I get a reference to a script that is on another gameobject
I am trying to disable a script that lets the player shoot so when the gun isn't equipped they can't shoot but it isn't letting me getcomponent with the name of Playerbullet. Can anyone help? using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class WhatIsEquipped : MonoBehaviour
{
public Image GunIcon;
public Image SwordIcon;
public GameObject Player;
// Start is called before the first frame update
void Start()
{
GunIcon.enabled = false;
SwordIcon.enabled = false;
Player.GetComponent<Playerbullet>().enabled = false;
}
// Update is called once per frame
void Update()
{
if(SwordIcon.enabled == true)
{
GunIcon.enabled = false;
}
if(GunIcon.enabled == true)
{
SwordIcon.enabled = false;
}
if (Input.GetKeyDown("2"))
{
ToggleSwordIcon();
}
if (Input.GetKeyDown("1"))
{
ToggleGunIcon();
}
}
void ToggleSwordIcon()
{
SwordIcon.enabled = !SwordIcon.enabled;
}
void ToggleGunIcon()
{
GunIcon.enabled = !GunIcon.enabled;
Player.GetComponent<Playerbullet>().enabled = true;
}
}
Comment