- Home /
Getting a Variable on Script B to equal a variable from script A
Hi all,
I've been digging through the reference manual and the answers page and I'm having a little trouble with getting a variable from another script. I have two different scripts. Script A is DoorScript and it has a variable influencenumber. Here is the script for that:
using UnityEngine;
using System.Collections;
public class DoorScript : MonoBehaviour {
public int doornumber;
public int influencenumber;
// Use this for initialization
void Start () {
if (gameObject.name == "Door 1"){
doornumber=1;
influencenumber=2;
}
if (gameObject.name == "Door 2"){
doornumber=2;
influencenumber=1;
}
if (gameObject.name == "Door 3"){
doornumber=3;
influencenumber=-1;
}
if (gameObject.name == "Door 4"){
doornumber=4;
influencenumber=-2;
}
}
There are four doors right now but each has a different influence number. I'm having an AI choose a door and then move to it and get the influence number of that door and categorize it. Here's what I have so far:
sing UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AIChoiceScript1 : MonoBehaviour {
public List<GameObject> good;
public List<GameObject> bad;
public List<GameObject> unknown;
public GameObject choice;
public int choiceinfluence;
// Use this for initialization
void Start () {
List<GameObject> good = new List<GameObject>();
List<GameObject> bad = new List<GameObject>();
List<GameObject> unknown = new List<GameObject>();
AddAllObjects();
}
// compiles a list of every object into the unknown list
public void AddAllObjects(){
GameObject[]go = GameObject.FindGameObjectsWithTag("Interactable");
foreach(GameObject Interactable in go)
AddTarget(Interactable);
}
public void AddTarget(GameObject Interactable){
unknown.Add(Interactable);
}
// Update is called once per frame
void Update () {
if (choice == null){
choice = unknown[Random.Range(0,unknown.Capacity)];
}
}
void OnCollisionEnter(Collision collision) {
if (collision.gameObject == choice) {
choiceinfluence= choice.GetComponent("DoorScript").influencenumber;
}
}
}
For now it has no problem adding all the objects into the list and making the choice as to which one it wants to go to. However I can't seem to get the script to access the variable influencenumber from the DoorScript and set choiceinfluence = influencenumber. I've tried several different methods with the GetComponents script but most of the links I'm finding don't have any c# references and the manual doesn't seem to have any either.Most of the errors I'm getting are telling me it's of the wrong type or that there is no way to access DoorScript on the gameobject. I'm a little lost here but any help would be appreciated! Thanks!
In c#, GetComponent is used like this
choiceInfluence = choice.GetComponent<DoorScript>().influencenumber;
Your answer
Follow this Question
Related Questions
Syntax troubles (GetComponent and changing variables) 1 Answer
Distribute terrain in zones 3 Answers
C# Access to a variable by another script attached in a gameObject in the scene. 2 Answers
Using a variable value with GetComponent 1 Answer
Calling variable accross scripts in C# problem (Closed) 1 Answer