Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Question about how to call components between scripts.

Discussion in 'Scripting' started by VengarlofForossa, Jan 1, 2023.

  1. VengarlofForossa

    VengarlofForossa

    Joined:
    Jan 1, 2023
    Posts:
    18
    RESOLVED:

    Code (CSharp):
    1.             PlayerController.Instance.activeWeapon = weaponThrowable.GetComponentInChildren<Weapon>();
    2.  
    With this line in ActiveWeapon, in the functions for call weapons

    Thank you all!
    ---------------------------------

    Hello!

    I have a doubt about how to call objects between scripts.

    I have a script with the stats of weapons and I attach this script to the weapon

    Code (CSharp):
    1. public class Weapon : MonoBehaviour
    2. {
    3.     public string gunName;
    4.     /*....*/
    5. }
    6.  
    In my PlayerController, I have assigned a script that has the character controls and in which I have assigned the weapon that is active.

    Code (CSharp):
    1.     [Header("Weapon")]
    2.     public Weapon activeWeapon;
    Inside PlayerController I have an object called Weapon Container, which has four objects assigned to it (Weapon one, two...) and inside each object is the prefab of the weapon.

    And I have another script attached to PlayerController called WeaponActive.

    Within the weaponactive script, I have functions assigned to the buttons. If I press 1, it activates WeaponOne and deactivates the others, if I press 2, it activates WeaponTwo and deactivates the others...

    My question is... How do I assign the gameobject inside WeaponOne, or weaponTwo... to the Player Controller script?

    My problem, rather than logic, is one of syntax when writing the code to do it.

    My weaponActive script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WeaponActive : MonoBehaviour
    6. {
    7.     public GameObject weaponOne;
    8.     public GameObject weaponTwo;
    9.     public GameObject weaponMelee;
    10.     public GameObject weaponThrowable;
    11.  
    12.     public PlayerController playerController;
    13.  
    14.     private void Awake()
    15.     {
    16.         setWeaponOne();
    17.     }
    18.  
    19.     private void Update()
    20.     {
    21.         setWeaponOne();
    22.         setWeaponTwo();
    23.         setWeaponMelee();
    24.         setWeaponThrowable();
    25.     }
    26.  
    27.     public void setWeaponOne()
    28.     {
    29.         if (Input.GetKeyDown(KeyCode.Alpha1) && !Pause.isPause)
    30.         {
    31.             weaponOne.SetActive(true);
    32.             weaponTwo.SetActive(false);
    33.             weaponMelee.SetActive(false);
    34.             weaponThrowable.SetActive(false);
    35.         }
    36.     }
    37.  
    38. /*.......*/
    39. }
    40.  
    That's the Hierarchy Objects.



    Thank you for your support!!
     
    Last edited: Jan 1, 2023
  2. dynamicbutter

    dynamicbutter

    Joined:
    Jun 11, 2021
    Posts:
    61
    There are lots of ways to do this. Given what you already have you could create a public method on WeaponContainer called GetActiveWeapon (or whatever) and have it return the currently active weapon.
     
  3. dynamicbutter

    dynamicbutter

    Joined:
    Jun 11, 2021
    Posts:
    61
    You might also consider dynamically instantiating your weapon prefabs.
     
  4. VengarlofForossa

    VengarlofForossa

    Joined:
    Jan 1, 2023
    Posts:
    18
    Yes... but my problem, rather than logic, is one of syntax when writing the code to do it. How can I do this? It's my principal doubt

    Code (CSharp):
    1.     public void setWeaponOne()
    2.     {
    3.         if (Input.GetKeyDown(KeyCode.Alpha1) && !Pause.isPause)
    4.         {
    5.             weaponOne.SetActive(true);
    6.             weaponTwo.SetActive(false);
    7.             weaponMelee.SetActive(false);
    8.             weaponThrowable.SetActive(false);
    9.  
    10.             //weaponOne Childs' to playercontroller activeWeapon
    11.  
    12.  
    13.         }
    14.     }
    Resolved!
    PlayerController.Instance.activeWeapon = weaponThrowable.GetComponentInChildren<Weapon>();

    In few minutes I change the first message of this thread
     
    Last edited: Jan 1, 2023