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 BaldiWonKenobi · Jul 30, 2014 at 11:20 PM · prefabnoobattach

unable to attach a script to a prefab

I have a prefab that I am using with a script attached to it. Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class bagDetect : MonoBehaviour {
     public GameObject okToShoot;
     public bool canScore = true;
     void Start () {
         okToShoot.GetComponent<ID355> ().okToScore = true;
     }
     void Update () {
         canScore = true;
     }
 }

the problem is i cannot seem to attach the reference to it. It will not let me drag and drop or assign the variable. This is the error i get when i run the game. Any ideas on what i am doing wrong?????

UnassignedReferenceException: The variable okToShoot of bagDetect has not been assigned. You probably need to assign the okToShoot variable of the bagDetect script in the inspector. UnityEngine.GameObject.GetComponent[ID355] () bagDetect.Start () (at Assets/bagDetect.cs:8)

Comment
Add comment · Show 7
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 rutter · Jul 30, 2014 at 11:20 PM 0
Share

You may need to edit the prefab instance, rather than the prefab itself.

The prefab's template can only contain references to assets or other objects internal to the prefab. You can only reference other scene objects once the prefab exists in a scene.

avatar image BaldiWonKenobi · Jul 30, 2014 at 11:25 PM 0
Share

how would i go about doing that?

avatar image BaldiWonKenobi · Jul 30, 2014 at 11:37 PM 0
Share

i think the issue is that the prefab is something that gets instantiated and there is none on the board until an event happens

avatar image BaldiWonKenobi · Jul 31, 2014 at 12:04 AM 0
Share

so is it possible to assign a variable to prefab before it is instantiated?

avatar image rutter · Jul 31, 2014 at 12:21 AM 0
Share

The prefab itself can't hold references to scene objects, because there's no clear way to handle that if the prefab is dropped into another scene.

If you need a reference to something in the scene, I'd recommend the following:

  • Drop a copy of the prefab into your scene, somewhere out of the way.

  • Select the instance and disable it (checkbox in the inspector, next to the GameObject's name).

  • Set whatever value you need to set.

Your Instantiate call can target the new instance that you just configured.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Meltdown · Jul 31, 2014 at 03:41 AM

What's happening is you're instantiating a prefab. This new prefab has no idea what okShoot is when its instantiated.

So what you need to do is something like this...

    // Instantiate prefab and get its script
     var instantiatedPrefab = GameObject.Instantiate(Resources.Load("PrefabName"));
     var bagDetectObj = instantiatedPrefab.GetComponent<bagDetect>();
     
     // Now set okShoot on the bagDetectScript on your instantiated prefab
     bagDetectObj.okShoot = GameObject.Find("okShootGameObject");

Comment
Add comment · Show 2 · 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_SA · Jan 15, 2018 at 01:21 AM 0
Share

How would you do this with UI elements? GameObject.Find wont work for me?

avatar image jdean300 Unity_SA · Jan 15, 2018 at 03:38 AM 0
Share

There is nothing special about UI elements. Why doesn't GameObject.Find work?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to attach a script to a static Tree Prefab? 1 Answer

making prefabs at runtime? 2 Answers

Generated meshes/materials cannot be made into prefabs? 1 Answer

Scripts of instantiated objects 1 Answer

Communicating between objects in the same prefab? 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