Hello Guest

Author Topic: Problem with setting a library dynamically  (Read 4606 times)

mrwallace

  • Newbie
  • *
  • Posts: 3
    • View Profile
Problem with setting a library dynamically
« on: August 20, 2013, 05:12:18 pm »
Hi all:

I want to change the sprite collection and get a clip in runtime.
I have search in the forum and there are some similar questions but i couldn't make my script work.
I have this:

private var tkScript : tk2dSpriteAnimator;
private var data : tk2dSpriteAnimation;

function Start () {
tkScript = gameObject.GetComponentInChildren (tk2dSpriteAnimator);
data= Resources.Load("TestChar", typeof(tk2dSpriteAnimation)) as tk2dSpriteAnimation;
tkScript.Library = data;
tkScript.Play("Test");

If i try with tk2dSpriteCollectionData i can pick sprites from my resources and it works, but when i try this to pick a clip its says Library not set on play windows in Unity

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Problem with setting a library dynamically
« Reply #1 on: August 20, 2013, 07:13:15 pm »
What is data returning? And you're definitely sure your TestChar sprite animation object is definitely in Resources, and its got a unique name? Make sure there isn't a gameobject in resources elsewhere in the project.

You can try something like this to help track this down:
Code: [Select]
GameObject go = Resources.Load("TestChar", typeof(GameObject)) as GameObject;
if (go != null) {
  UnityEditor.Selection.activeGameObject = go;
}
else {
  Debug.Log("Not found. Are you sure its in resources?");
}

mrwallace

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Problem with setting a library dynamically
« Reply #2 on: August 21, 2013, 10:41:13 am »
Thank you for your answer i found where is the problem but couldn't find the solution


private var tkScript : tk2dSpriteAnimator;
private var dataGo : tk2dSpriteAnimation;
var go : GameObject;

function Start () {


tkScript = gameObject.GetComponentInChildren (tk2dSpriteAnimator);
//i found this script properly

go = Resources.Load("AlienBlueAnim", typeof(GameObject)) as GameObject;
if (go != null) {
  UnityEditor.Selection.activeGameObject = go;
  Debug.Log ("getting " + go);
// i get AlienBlueAnim
}
else {
  Debug.Log("Not found. Are you sure its in resources?");
}


dataGo = go.GetComponentInChildren(tk2dSpriteAnimation);
if (dataGo != null)
   {
Debug.Log ("the value of  dataGo es" + dataGo);
   }
else
   {
   Debug.Log ("not found the spriteAnimation");
   
   }

// i dont found the script


tkScript.Library = dataGo;

tkScript.Play("heart2");



I found that the problem is that i'm not getting the script from the gameObject. I assume to set the library i have to  assign the SpriteAnimation component to the library.
I don't get any errors in compile time but when i press play says Library not set and clip not found.
i have used thousand of times getComponentInChildren and i am a little lost what im doing wrong. if i found the gameObject in Resources , i should be able to find the script i think.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Problem with setting a library dynamically
« Reply #3 on: August 21, 2013, 10:55:23 am »
You don't need GetComponentInChildren for that, just GetComponent<...> should do.
Also, using that debug code I posted before, have you actually find the correct object?

mrwallace

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Problem with setting a library dynamically
« Reply #4 on: August 21, 2013, 12:47:11 pm »
yes i had the correct Object.

I solved the problem changing GetComponentInChildren for GetComponent.
I thought the first one look in all the parent and children, in fact in the other gameObject works to pick the tk2dSpriteAnimator

Thank you very much the final code is this : maybe could be useful for future readers


private var tkScript : tk2dSpriteAnimator;
private var dataGo : tk2dSpriteAnimation;
var go : GameObject;

function Start () {


//store the script tk2dSpriteAnimator to manipulate
tkScript = gameObject.GetComponentInChildren (tk2dSpriteAnimator);

//Load the Collection we want to load through the gameObject . Has to be in Resources folder
go = Resources.Load("AlienBlueAnim", typeof(GameObject)) as GameObject;
if (go != null) {
  UnityEditor.Selection.activeGameObject = go;
  Debug.Log ("getting " + go);
}
else {
  Debug.Log("Not found. Are you sure its in resources?");
}


//pick the Animation component to set the library
dataGo = go.GetComponent(tk2dSpriteAnimation);
if (dataGo != null)
   {
Debug.Log ("el valor de dataGo es" + dataGo);
   }
else
   {
   Debug.Log ("not found the spriteanimation");
   
   }


//set the library
tkScript.Library = dataGo;


//set the clip
tkScript.Play("AlienBlue1");