- Home /
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?
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
Hi hollym can you tell me please how can I build scripts directly to the asset bundles ? thks
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
Follow this Question
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