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 /
avatar image
0
Question by N.Klawitter · Mar 15, 2015 at 07:11 PM · scripting problemaccess

Access a method from another script AND different GameObject

Hey everyone.

I use the getComponent<>() method to access other scripts successfully in this project but it doesn't seem to work if the scripts are not attached to the same object. I'm getting a NullReferenceException every time it gets down to

 nextPos = getXml.getPositionData (onSet.ToString (), myName);

What am I doing wrong here? my other script GetXML is attached to a separate gameobject. Many thanks.

 using UnityEngine;
 using System.Collections;
 
 public class QuickMove : MonoBehaviour 
 {
     public Vector3 currentPos;
     public Vector3 nextPos;
     public GetXML getXml;
     private int onSet = 1;
     private string myName;
 
     // Use this for initialization
     void Awake ()
     {
         getXml = GetComponent<GetXML> ();
 
     }
     void Start () 
     {
         currentPos = transform.position;
         myName = transform.name;
     }
     
     // Update is called once per frame
     void Update () 
     {
 
         if (Input.GetKeyDown ("2") && (onSet <= 29)) 
         {
             currentPos = transform.position;
             onSet++;
         
             nextPos = getXml.getPositionData (onSet.ToString (), myName);
             transform.position = Vector3.Lerp (currentPos, nextPos, Time.deltaTime * 3.0f);
         }
     }
 }
 
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Socrates · Mar 15, 2015 at 11:16 PM

If I am reading your question above correctly, you want your QuickMove script to access the GetXML script that is located on an object other than the one the QuickMove script is located on.

The getComponent() method returns the component of Type on the same game object as the calling script. That is why your posted code is not working correctly.

What you need is a reference to the game object that holds the GetXML component. You may want to set this with a public variable, have Awake() find it based on a tag, or maybe you're using a singleton pattern. Whatever works for your project. Once you have a reference to this game object, you can use GetComponent from that reference.

 // If you store a GameObject.
 getXml = someGameObject.GetComponent<GetXML>(); 
 
 // Or if you store a Transform.
 getXml = someTransform.gameObject.GetComponent<GetXML>();


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

Answer by Denvery · Mar 15, 2015 at 07:21 PM

If you use GetComponent(), it tries to get this script on current GameObject. You can simply assigh GameObject with attached GetXML.cs to the inspector's field, because you have field

public GetXML getXml;

which is visible in inspector for QuickMove script.

In this case you no need to use getXml = GetComponent ();, you can use getXml without null exceptions

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

Answer by N.Klawitter · Mar 15, 2015 at 11:23 PM

Luckily I solved my problem before it got posted :) Attaching in the inspector wasn't working for me however, perhaps because these are prefabs instantiated by the GetXML script.

Anyways I just needed to find the parent object of GetXML.

 using UnityEngine;
 using System.Collections;
 
 public class QuickMove : MonoBehaviour 
 {
     public Vector3 currentPos;
     public Vector3 nextPos;
     public GetXML getXml;
     public float speed = 100;
     private int onSet = 1;
     private string myName;
     
     void Awake ()
     {
         // Find the GetXML script attached to "DataObject"
         getXml = GameObject.Find ("DataObject").GetComponent<GetXML>();
     }
 
     void Start () 
     {
         currentPos = transform.position;
         nextPos = transform.position;
         myName = transform.name;
     }
     
 
     void Update () 
     {
         PositionChanging ();
     }
 
     void PositionChanging()
     {
         // Lerp to nextPos when nextPos changes.
         if (Input.GetKeyDown ("2") && (onSet < 30)) 
         {
             onSet++;
             nextPos = getXml.getPositionData (onSet.ToString (), myName);
         }
         if (Input.GetKeyDown ("1") && (onSet > 1)) 
         {
             onSet--;
             nextPos = getXml.getPositionData (onSet.ToString (), myName);
         }
 
         // Fast in slow out.  Needs a gate on the finish to get to nextPosition still.
         transform.position = Vector3.Lerp (transform.position, nextPos, speed * Time.deltaTime * 10.0f);
     }
 }

I also fixed the lerp to update every frame instead of only when keyboard input was true.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Accessing one script on multiple gameObjects? 2 Answers

A Javascript trying to access an image effect C# script 1 Answer

I want to access bool component of my one script in another script but i am not able to do , please help me its urgent 0 Answers

How do I access two Scripts 1 Answer

Works after restart then crashes again ! "There is no 'Collider attached... a script is trying to access it" 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