Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by cidmodder · Oct 14, 2013 at 08:13 PM · c#aigetcomponentvariables

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Doireth · Oct 14, 2013 at 08:21 PM 3
Share

In c#, GetComponent is used like this

 choiceInfluence = choice.GetComponent<DoorScript>().influencenumber;

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges