Hello Guest

Author Topic: Assign a game object dynamically  (Read 6932 times)

Benjam901

  • Newbie
  • *
  • Posts: 12
    • View Profile
Assign a game object dynamically
« on: November 06, 2013, 11:02:24 pm »
Hello,

I have been trying to figure out how to assign my character to the game object at runtime. I am adding a new script and need to assign the game objects in the start function but I cannot seem to get it to work.

This is what I have so far:
Code: [Select]
void start()
{
spriteID = sprite_Character.GetClipIdByName("Library_Walking");
spriteCollection = Resources.Load ("Collection_WalkCycle", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
sprite_Character = GetComponent<tk2dSpriteAnimator>();
sprite_Character = tk2dSpriteAnimator.AddComponent(GameObject.Find("Character"), spriteCollection, spriteID);
}

The errors I am getting as as follows:

Assets/Scripts/Character_Interaction/Base_Interaction.cs(45,55): error CS1503: Argument `#2' cannot convert `tk2dSpriteCollectionData' expression to type `tk2dSpriteAnimation'

Also I am not sure if I fully understand/am using int spriteID correctly. Can someone explain how this works please and if I should use a collection or a library for this :)

Thanks!

:)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Assign a game object dynamically
« Reply #1 on: November 07, 2013, 10:11:11 am »
Are you trying to create a character at runtime? If you don't need to customise it, it might be easier to simply instantiate a prefab. Wouldn't that do what you need?

Benjam901

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Assign a game object dynamically
« Reply #2 on: November 07, 2013, 11:44:17 am »
No, what I am doing is removing one script from the character and assigning another. But as I am assigning another I need to tell unity to use the character as the game object and the transform.

Usually I would use character = gameobject.find("character"); or just assign the character game object in the inspector to do this, but 2D toolkit needs to use addcomponent if you are doing this in script.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Assign a game object dynamically
« Reply #3 on: November 07, 2013, 11:55:27 am »
Are you trying to add an animator to GameObject.Find("Character")?
You're also getting a sprite collection, which is unnecessary for an animator...

AddComponent(..., sprite_Character.Library, spriteCharacter.GetClipIdByName("...")); should do it I guess?

Benjam901

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Assign a game object dynamically
« Reply #4 on: November 09, 2013, 04:45:04 pm »
What I am trying to do is to assign the actual sprite to the game animator for it to reference its height and position etc.

See image with arrow pointing to what I need to assign



I also need to change the sprite collection which I am working on at the moment. I just need these 2 things then I can carry on with the project :)

The code for the start function (Which doesn't seem to be doing much at the moment) Is as follows:

Code: [Select]
void start()
{
//characterGameObj = new GameObject.Find("Character");
print(characterGameObj);
//sprite_Character.Library = Resources.Load("MovementLib", typeof(tk2dSpriteAnimation)) as tk2dSpriteAnimation; //sets a library(For some reason it doesnt work, but it works with other libraries)???
spriteID = sprite_Character.GetClipIdByName("Library_Walking");

spriteCollection = Resources.Load ("Collection_WalkCycle", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData; //this must be of type Data to work properly when passing it into sprite_Character
sprite_Character = GetComponent<tk2dSpriteAnimator>();
sprite_Character = tk2dSpriteAnimator.AddComponent(GameObject.Find("Character"), sprite_Character.Library, sprite_Character.GetClipIdByName("Anim_WalkingClip"));
}

Any thoughts?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Assign a game object dynamically
« Reply #5 on: November 09, 2013, 07:09:30 pm »
I still don't understand what you're trying to do. You're getting the component on yourself, but what is "Character"? Isn't base_interaction attached directly to that object? Why aren't you simply dragging a reference to it?
I think I'm missing something here.

Benjam901

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Assign a game object dynamically
« Reply #6 on: November 09, 2013, 07:43:24 pm »
Character is the name of the sprite game object in unity.

What I am doing is using one script to handle the getting out of bed stuff and switching between animations and when he gets out of bed the script is destroyed and the walking script is attached. I need to assign the animated sprite an object reference because I am gettign this error:

NullReferenceException: Object reference not set to an instance of an object

Benjam901

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Assign a game object dynamically
« Reply #7 on: November 09, 2013, 08:19:54 pm »
I have managed to change the collection but it is now throwing me this error:
IndexOutOfRangeException: Array index is out of range.

Something about materials? Could this be because my sprite collection is on multiple atlases?

Benjam901

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Assign a game object dynamically
« Reply #8 on: November 09, 2013, 09:15:36 pm »
My clip ID is returning as -1? what does this mean and how should I look into fixing it it is a problem?

Here is the full error:

IndexOutOfRangeException: Array index is out of range.
tk2dSprite.UpdateMaterial () (at Assets/TK2DROOT/tk2d/Code/Sprites/tk2dSprite.cs:202)
tk2dBaseSprite.SetSprite (.tk2dSpriteCollectionData newCollection, Int32 newSpriteId) (at Assets/TK2DROOT/tk2d/Code/Sprites/tk2dBaseSprite.cs:231)
tk2dSpriteAnimator.SetSprite (.tk2dSpriteCollectionData spriteCollection, Int32 spriteId) (at Assets/TK2DROOT/tk2d/Code/Sprites/tk2dSpriteAnimator.cs:729)
Getting_Up.changeLibrary (System.String nameOfLib, System.String nameOfCol) (at Assets/Scripts/Character_Interaction/Getting_Up.cs:108)
Getting_Up.returnToIdle (.tk2dSpriteAnimator sprite, .tk2dSpriteAnimationClip clip) (at Assets/Scripts/Character_Interaction/Getting_Up.cs:46)
tk2dSpriteAnimator.OnAnimationCompleted () (at Assets/TK2DROOT/tk2d/Code/Sprites/tk2dSpriteAnimator.cs:719)
« Last Edit: November 09, 2013, 09:17:15 pm by Benjam901 »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Assign a game object dynamically
« Reply #9 on: November 10, 2013, 02:39:42 pm »
clipId == -1 means the clip isn't found. Isn't the clip name "Anim_WalkingClip"? You're searching for Library_Walking...

Benjam901

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Assign a game object dynamically
« Reply #10 on: November 11, 2013, 07:22:52 pm »
Aha! All sorted now :)

I had the setID line before I had set the new library which was why it was not doing it properly. Cheers ofr the help!