Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 rrtkd · May 13, 2019 at 03:44 PM · scripting beginnernullreferenceexceptioncommunicationbasics

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.

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

Object not responding to OnTriggerEnter() 2 Answers

PlayFab CloudScript NullReferenceException 0 Answers

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

Nested Class Initialization 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