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 /
avatar image
0
Question by unity_r2Br3sLe2Gd37w · Apr 11, 2020 at 07:24 PM · scripting problemreferencereferencing

Drag and drop reference not working?

I made a reference to a script and I was going to go ahead and drag the script into the slot, but it didn't let me do so. Turns out, nothing goes into that slot. Any help? Here's the script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Tutorial : MonoBehaviour
 {
     public levelMenu levelMenu;
     public void Start()
     {
         if(levelMenu.tutorialStart)
         {
             Debug.Log("tutorial");
         }
     }
 }
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

Answer by kirbygc00 · Apr 11, 2020 at 08:30 PM

I would generally use a scriptable object. it's a great way to serialize data and supply it to scripts.

However to answer your specific question, try instantiating the script on some gameobject in the scene, then drag that into the Tutorial's levelMenu field.

alt text alt text


assigned.jpg (15.9 kB)
player.jpg (24.4 kB)
Comment
Add comment · Show 3 · 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 unity_r2Br3sLe2Gd37w · Apr 12, 2020 at 12:10 AM 0
Share

Thank you very much! This has definitely helped, but what if the game object with the script is in a different scene, would it still be possible to do this? @kirbygc00

avatar image kirbygc00 unity_r2Br3sLe2Gd37w · Apr 12, 2020 at 08:29 PM 0
Share

Yup! In cases where the game object is not readily available in the scene I generally grab the script reference through code. So I would do something like:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class Tutorial : $$anonymous$$onoBehaviour
  {
      public level$$anonymous$$enu level$$anonymous$$enu;
      public void Start()
      {
         // on start this reference would be null, so we need to assign it
          if (level$$anonymous$$enu == null) 
               // look through objects in scene to find one which contains the
              // level$$anonymous$$enu component, then assign that component as the reference
                level$$anonymous$$enu = FindObjectOfType<level$$anonymous$$enu>();
      }
  }

I don't believe it is possible to grab a script which isn't loaded into the current scene through this method. In case you do need this kind of behaviour, I would use the Resources.Load method to dynamically load it in: https://docs.unity3d.com/ScriptReference/Resources.Load.html using System.Collections; using System.Collections.Generic; using UnityEngine;

   public class Tutorial : $$anonymous$$onoBehaviour
   {
       public level$$anonymous$$enu level$$anonymous$$enu;
       public void Start()
       {
          // on start this reference would be null, so we need to assign it
           if (level$$anonymous$$enu == null) 
                // grab the script from your "resources" folder, you may need to also instantiate a gameobject with the script loaded as a component... just wrap this call in an instantiate method.
                 level$$anonymous$$enu = Resources.Load<level$$anonymous$$enu>("path/to/object");
       }
   }
avatar image Cubemann · Mar 25, 2021 at 11:31 PM 0
Share

Help I have this problem too.

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

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

Duplicating Component with Some Scene References in Serialized Scriptable Object Class Reference 0 Answers

One Script Referred by Another Script used by Multiple Objects 0 Answers

Object reference not set to an instance of an object (Scripts calling objects which only exist in certain scenes) 1 Answer

Losing object reference 2 Answers

Peculiar Bleed-through of Exposed References on different Timeline Tracks 2 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