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 hollym16 · Aug 05, 2014 at 02:48 PM · gameobjectassetbundlenullreferenceexception

Using Scripts in AssetBundles

Hi!

I've created a project where when buttons are pressed, a different model comes up, this is a cut down version of my script:

 #pragma strict
 
 private var ray: Ray;
 private var hit: RaycastHit;
 
 private var bombard : GameObject;
 private var constrict : GameObject;
 
 function Start () {
 
 bombard = GameObject.Find("ImageTarget/GameObject/Bombarding machine");
 constrict = GameObject.Find("ImageTarget/GameObject/Constrictor");
 
 bombard.SetActive(false);
 constrict.SetActive(false);
 }
 
 function Update () {
 
 if(Input.GetMouseButtonDown(0)){
     ray=Camera.main.ScreenPointToRay(Input.mousePosition);
     if(Physics.Raycast(ray, hit)){
     if(hit.transform.name == "Bom HL"){
     bombard.SetActive(true);
     constrict.SetActive(false);
     
     }
     if(hit.transform.name == "Cons HL"){
     bombard.SetActive(false);
     constrict.SetActive(true);
 }
 }
 }
 }

The script was originally on the Empty GameObject, within the ImageTarget (Augmented Reality) but when I created an AssetBundle out of the Empty GameObject (and it's contents) the script wouldn't work, thus all the models were active on start up instead of only when clicked. Basically, the script isn't working.

I then tried to go back and put the script on the Image Target, which isn't in the AssetBundle, and re-bundle it. However, it did the same thing. On both occasions, when I clicked the buttons, it came up with a NullReferenceException.

Is there a bit of script I need to put in in order to tell the AssetBundle to apply the script? And should it go in the AssetBundle or remain outside of it?

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
0
Best Answer

Answer by hollym16 · Aug 06, 2014 at 09:12 AM

For anyone else who's struggling with this, I found that scripts can be used within asset bundles but they must be built directly into them. Also if using the GameObject.Find function, don't do a long path through your project; try and put it on the parent object so you only have to reference the name of the asset you need to find. This then prevents the NullReferenceException

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 qingruibin · Aug 06, 2014 at 12:04 PM 0
Share

script see$$anonymous$$gly not in AssetBundles.

avatar image Fabrica · Jul 22, 2015 at 05:55 PM 0
Share

Hi hollym can you tell me please how can I build scripts directly to the asset bundles ? thks

avatar image hollym16 · Jul 23, 2015 at 09:00 AM 0
Share

I don't think there's any special way of doing it. Thus far I've only used simple scripts (such as SetActive different objects) but used them in the normal way. If you put everything in a parent object, an Empty GameObject, and use all the scripts within that, it should work when you make that parent the asset bundle.

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

22 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

Related Questions

How to import the object from server to unity 2 Answers

How to Destroy a gameobject on collision 3 Answers

How to make a gameobject shake. 1 Answer

How can I access other scripts and their functions? 3 Answers

C# GetComponent Issue 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