Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by SullyCreates · Feb 20, 2018 at 12:05 AM · textcharactercontrollergetcomponentvariables

Accessing a variable using Get Component from other GameObject Script

I have two scripts.

One script controls the player's movement and detects the collision between GameObjects such as NPCs, doors, etc:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     public float speed = 6.0f;
     public float turnSpeed = 60.0f;
     public float gravity = 20.0f;
     public bool canDo;
 
     private Vector3 moveDirection = Vector3.zero;
     private CharacterController controller;
     private Animator anim;
 
     void Start ()
     {
         controller = GetComponent<CharacterController> ();
         anim = GetComponent<Animator> ();
     }
 
     void Update ()
     {
         if (canDo == true)
         {
             if (controller.isGrounded)
             {
                 moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0.0f, Input.GetAxis ("Vertical"));
             }
                 
              Vector3 facingrotation = Vector3.Normalize (new Vector3 (Input.GetAxis ("Horizontal"), 0f, Input.GetAxis ("Vertical")));
 
             if (facingrotation != Vector3.zero)
             {
                 transform.forward = facingrotation;
             }
 
             controller.Move (moveDirection * speed * Time.deltaTime);
             moveDirection.y -= gravity * Time.deltaTime;
 
             if (Input.GetKey ("up") || Input.GetKey ("down") || Input.GetKey ("left") || Input.GetKey ("right"))
             {
                 anim.SetInteger ("isMoving", 1);
             } else {
                 anim.SetInteger ("isMoving", 0);
             }
         }
     }
 
     void OnControllerColliderHit (ControllerColliderHit other)
     {
         if (other.gameObject.CompareTag ("Door"))
         {
             if (Input.GetKeyDown ("space"))
             {
                 other.gameObject.SetActive (false);
             }
         }
 
         if (other.gameObject.CompareTag ("NPC"))
         {
             if (Input.GetKeyDown ("space"))
             {
                 
             }
         }
     }
 }

And my other script, which controls what the NPC says (I have another already functioning script that does dialogue). It detects which order the text should be displayed based upon the variable whichDialogueBox:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NPCTestSpeechController : MonoBehaviour {
 
     public DisplayTextController myDisplayTextController;
     public GameObject dialogueSystem;
     public PlayerController myController;
 
     void Start ()
     {
         myDisplayTextController.whichDialogueBox = 0;
     }
 
     void Update ()
     {
         if (myDisplayTextController.whichDialogueBox == 1)
         {
             myController.canDo = false;
             dialogueSystem.SetActive (true);
             myDisplayTextController.nameString = "Jimothy";
             myDisplayTextController.speechString = "'Ello! 'ello!";
         }
 
         if (myDisplayTextController.whichDialogueBox == 2)
         {
             dialogueSystem.SetActive (false);
             myDisplayTextController.whichDialogueBox = 0;
             myController.canDo = true;
         }
     }
 }
 

How do I go about accessing the scripts variable whichDialogueBox and changing it. This would need to be changeable so that it can be used for all initiation of NPC action. I thought GetComponent may work, accessing the script after checking the tag to see if it's an NPC and then editing the variable, but I can't figure it out. I ask for a bit of help and suggestions of how to go about this. Am I doing this wrong? Thanks!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · Feb 20, 2018 at 09:57 AM

I would save a reference to the npcSpeechController hit by your controller, and call the appropriate function in the Update since OnControllerColliderHit is called one frame only

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class PlayerController : MonoBehaviour
  {
      // ...
      
      private NPCTestSpeechController  npcSpeechController;
  
      void Update ()
      {
          if (canDo == true)
          {
              // ...
              
              if( npcSpeechController != null )
              {
                  // Make sure the npcSpeechController is still in range
                  // Change the '10' threshold according to your needs
                  if( (npcSpeechController.transform.position - transform.position).sqrMagnitude > 10 )
                       npcSpeechController = null ;
                  else if( Input.GetKeyDown ("space") )
                      npcSpeechController.whichDialogueBox++ ;
              }
          }
      }
  
      void OnControllerColliderHit (ControllerColliderHit other)
      {
          if (other.gameObject.CompareTag ("Door"))
          {
              if (Input.GetKeyDown ("space"))
              {
                  other.gameObject.SetActive (false);
              }
          }
  
          else
          {
              // May be null, but it does not matter
              // You may need to use GetComponentInParent instead of GetComponentInChildren
              npcSpeechController = other.GetComponentInChildren<NPCTestSpeechController>();
          }
      }
  }
Comment
Add comment · Show 5 · Share
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 SullyCreates · Feb 21, 2018 at 09:47 PM 0
Share

I can see this, yes. This may work. I'm trying to access a general script by using get component so I only need to write something of this fashion once, and then trigger whatever the NPC does. I'll try this. Thanks.

avatar image SullyCreates · Feb 21, 2018 at 10:32 PM 0
Share

It worked! Thank you very much! A quick question: How do I make it so you don't have to continuously walk into the NPC?

avatar image Hellium SullyCreates · Feb 21, 2018 at 10:52 PM 0
Share

What do you mean by "continuously walk into the NPC"?

avatar image SullyCreates Hellium · Feb 22, 2018 at 02:04 AM 0
Share

I have to continuously press the arrow keys as I collide with the NPC to register the trigger.

Show more comments

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

91 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 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 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 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 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 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 avatar image

Related Questions

Accessing a text variable from another script 0 Answers

Reserve ammo not displaying on UI 0 Answers

Disable function in another script 1 Answer

{SOLVED, but not realy} working on a space shooter game and im having problems with my upgrade buttons. 1 Answer

Pass Variable Through C Script 0 Answers


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