• 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 rrtkd · May 13, 2019 at 03:44 PM · nullreferenceexceptionscripting beginnercommunicationbasics

Access the content of another script

So... I am trying to go through some old unity tutorials, trying to make sort of a quiz game... The thing is ... they do it by getting scripts to communicate with one another. I tried going back, tried to see if the most basic code possible can communicate with each other.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ScriptA : MonoBehaviour
  {
    public ScriptB other;
 
    void Update() {
        other.DoSomething();
  }
 }

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ScriptB : MonoBehaviour
 {
     public void DoSomething()
     {
         Debug.Log("Hi there");
     }
 }

Well... The thing is... It did not work. I created 2 empty objects in an empty 2D scene, attached ScriptA to one, attached ScriptB to the other, dragged the empty object with ScriptB into the field of ScriptA ... And it displayed nothing. I am not sure what I did wrong. Please help.

Comment
Add comment · Show 3
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 k234234w · May 14, 2019 at 12:37 AM 0
Share

That should be all you have to do. Can you add a start function to each of your scripts, and have a log in them. $$anonymous$$aybe your objects are not active?

avatar image Superdsgross k234234w · May 14, 2019 at 02:12 AM 0
Share

Yeah it should work exactly how it is. When you look at object A in the inspector does it say object B in the script B field?

avatar image Hellium · May 14, 2019 at 08:38 AM 0
Share

Are you sure you have enabled the log in the console? ($$anonymous$$ake sure the white bubble at the top right corner of the console is checked).

Try with another function

  private GameObject cube;
  public void DoSomething()
  {
      if( cube == null ) cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
      cube.transform.position = Vector3.one * $$anonymous$$athf.Sin( Time.time ) ;
  }

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by Cornelis-de-Jager · May 14, 2019 at 05:22 AM

The problem is that you dragged the Empty Object with ScriptB onto ScriptA. This will not work because ScriptA is expecting a ScriptB not a Object. Instead change the code as follow:

 public class ScriptA  {

     public Transform ScriptBObj;
     ScriptB other;
     
     void Start () {
         other = ScriptBObj.GetComponent<ScriptB>();
         other.DoSomething();
     }    
 }

If you really need to attach ScriptB to ScriptA directly - you need to have two inspectors, lock the one. Then drag the Script NOT the empty object, onto the field.

Comment
Add comment · Show 1 · 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 Hellium · May 14, 2019 at 08:33 AM 1
Share

It's exaclty what you must not do.

Having public ScriptB other; and dropping the gameObject holding the component ScriptB is the correct way. Unity will automatically retrieve the reference to the component this way.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

117 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 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

NullReferenceException Error on first frame, despite everything functioning correctly 1 Answer

Beginner using Unity Playground Project: Can I end scene/game when a score is reached using the condition scripts? 0 Answers

Object not responding to OnTriggerEnter() 2 Answers

PlayFab CloudScript NullReferenceException 0 Answers

Random: NullReferenceException: Object reference not set to an instance of an object 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges