How to access classes in Unity 3D (C#)

Asked

Viewed 5,035 times

0

I’m making a 3D game in Engine Unity, an FPS to be more specific and taking advantage of this experience to learn programming in C#, the one I’m having in school.

But now I’ve come across a problem, which I know is simple to solve, but since I haven’t studied the language in depth, and I only master the basics of C, I have no idea how to solve.

I have, for this problem, 2 Scripts: The weapon and the character itself (FPS Controller). I want, through the character’s script to access the class "Pistol" which is that of my gun script, however, when I declare:

private Pistol arma;

He makes the following mistake:

"Assets/Standard Assets/Characters/Firstpersoncharacter/Scripts/Firstpersoncontroller.Cs(46,25): error CS0246: The type or namespace name `Pistol' could not be found. Are you Missing a using Directive or an Assembly Reference?"

I understand what this mistake is, I know what’s wrong, but I don’t know how it’s right.

PS.: If necessary, I can put my code to better view the error.

2 answers

1

From what I understand, you have the two scripts, Gun and Player. If they are part of the same Gameobject (your player/FPS Controller), you can take the script as follows:

private Pistol arma;
void Start(){
    arma = this.GetComponent<Pistol>();
}

Remembering that you say: "Pistol class" which is that of my gun script" The class and script have to have the same name, for example: if your class calls Pistol, the C# script has to call Pistol too, not Gun.

Now, if your Pistol script is on another object, for example on the weapon itself, you can tag it and in the script do the following:

private Pistol arma;
void Start(){
    arma = GameObject.FindWithTag("arma").GetComponent<Pistol>();
}

I think this will help. You’ll be able to reference the outside classes. Good luck, hugs

1

Look, I only know the method of instantiating a script with another, so you can access a Pistol method in Player for example.

//Pistol
using UnityEngine;
using System.Collections;

public class Pistol : MonoBehaviour {

//Instancia Pistol para ficar acessivel a outros Scripts
public static Pistol Instance;
private bool testa = false;

void Awake(){
    Instance = this;
  }
void Start(){
}
public void MetodoTeste(){
    testa = true;
  }
}


//Player
using UnityEngine;
using System.Collections;

public class Player : MonoBehavior{

//Instancie Player para poder receber Pistol
public static Player Instance;

void Awake(){
    Instance = this;
  }

void Start () {
    Pistol.Instance.MetodoTeste ();
  }
}

With this you access the 'Metodoteste' of the Pistol script through the Player script. When you instantiate a script always declare it as public Static Instance name, so it is accessible to other scripts. Remember to instantiate the two scripts, Pistol and Player (who received Pistol).

I hope it helps in what you need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.