Hello how can i add GameObject to arrays from the other script i dont know how can I reference it
Umm I’m not sure either, but to access an array in another script,
-
Ensure that both the class with the array in it as well as the array has public in front of it.
-
If the script with the array in it is named Grevyard, then do
public Grevyard grevyardScript; go back to the editor’s inspector and drag the Grevyard into the variable you created. -
Then do Grevyard.weapons[0] or something idk to access that array. Pretty sure you’ve been around longer than me but never hurts to check:smile:
this first gun script
public Transform me;
public static GameObject gun;
public bool distance1;
void Start(){
}
void Update(){
if(Vector3.Distance(transform.position,me.position)< 2.5f){
mesafe1=true;
}
if(distance1 && Input.GetKey(KeyCode.F)){
gun=Grevyard.weapons[0];}
(This can be done by pressing “Insert Code” icon while highlighting your code.)
- Assuming you actually mean to add the
gameObject guninto that arrayGrevyard.weapons[0]and not the other way around, you should do Grevyard.Weapons[0] = gun; and not gun = Grevyard.Weapons[0];
For example, if you want to define x as 1, you should put x = 1 and not 1=x. You did it as 1 = x in your code.
-
Have you dragged and drop the gameobject prefab “gun” into the variable gun?
public static GameObject gun;
not sure what the static is for, I think it’s fine i’m still inexperienced haha. Just remove “static” if not sure.
mesafe1=true;
}```
What is mesafe1? Do you mean distance1? Change it to
if(Vector3.Distance(transform.position,me.position)< 2.5f)
{
distance1=true;
}```
{
distance1=true;
}```
While this may look fine at first, imagine this scenario.
The player walks within 2.5 units of the gun. distance1 = true, like in the script. Yep, yep.
Then, without picking the gun up, they walk 100 units away. distance1 will still be true, according to your script. But common sense says that the player is too far away from the gun. What you need to do is to make it false when the player walks far enough away.
if(Vector3.Distance(transform.position,me.position)< 2.5f)
{distance1 = true;}
else
{distance1 = false;}
5. Have you defined distance1 to be false at the start?
```public Transform me;
public static GameObject gun;
public bool distance1;```
Not necessary, but good to do. If you follow the solution in "4.", you won't need to the script will do it for you haha.
6. You didn't misspell the name Grevyard right? If your other script is called Graveyard, that can cause an error.
7. You didn't create a variable for Grevyard. Look at "1., 2., 3." above. The script won't know what Grevyard is.
8. Have you defined "me"? Who is "me" to be exact? Is it the player, or the gameobject the script is in? If it is the player, name it player haha. And make sure to drag the transform component of "Player" into the variable too to define it fully instead of leaving it blank.
9.
```csharp
//assuming you did "using Unity.Engine" or something like that at the start of the script.
public Transform player; //Renamed "me" to "player"
public GameObject gun; //technically i don't think you need this.
public bool distance1 = false;
public Grevyard grevyardScript; //defines the variable as a script of the same type as Grevyard
void Update(){
if(Vector3.Distance(transform.position, player.position)< 2.5f) distance1=true;
else distance1 = false;
if(distance1 && Input.GetKey(KeyCode.F))
grevyardScript.weapons[0] = gun;
//You can try grevyardScript.weapons[0] = this.GameObject; instead, if the script is sitting on the gun itself.
}
If the script is not sitting on the gun itself… transform.position won’t work the way you want it to.
Assuming you make the gun disappear after you pick it up Destroy(this.gameObject); or something, remember that almost all your references will disappear. Either make a separate “GunManager” or “Guns” gameobject and script to manage your guns, or use
void Start()
{
player = GameObject.FindWithTag(“Player").GetComponent<Transform>;
//Remember to tag your Player as "Player", or use GameObject.Find("name of Player") instead
//You don't need to use public GameObject Gun; if you use grevyardScript.weapons[0] = this.GameObject; assuming //the script is sitting on the gun. But just in case you have an error,
gun = GameObject.FindWithTag("Gun"); //remember to tag your Gun as Gun, or use GameObject.Find("Gun");
grevyardScript = FindObjectOfType<Grevyard>();
//There's better ways of doing this(GameObject.FindWithTag or GameObject.Find, but since I don't know much about //your game I can only do this.
}
Of course, remember that both Grevyard and weapons[ ] must be set to public.
I might have made a few mistakes if so just post the whole script and the errors that Unity gives in this thread or in another thread thanks:)
All the best for your game!
Oh right next time post this in “Scripting”, like you did for your previous thread. That is correct, don’t change it. This “Documentation” is wrong I think, and you’ll probably get less replies here too : D
To insert code, do [ICODE] Your Code here [/ICODE] or press the “Inline Code” or “Insert Code” icons.