- Home /
The question is answered, right answer was accepted
Changing the sprite of an instantiated object through a collision box from another gameobject.
Basically I want to change the script of my projectile when touching a powerup and I have the following code:
using UnityEngine; using System.Collections;
public class BoneShooting : MonoBehaviour { private Animator anim; public GameObject shot; public Transform shotSpawnLeft; public Transform shotSpawnRight; public Transform shotSpawnUp; public Transform shotSpawnDown; public BoneScript boneInstantiate; public Collider2D powerup; public SpriteRenderer sprChanger; public SpriteRenderer sr; public Sprite[] sprArray; public float burstDelay = 0.3f; private float lastFireTime;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
powerup = GetComponent<Collider2D>();
sprChanger = GetComponent<SpriteRenderer>();
sr = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update ()
{
if (Input.GetKey(KeyCode.LeftArrow) && Time.time > (lastFireTime + burstDelay))
{
lastFireTime = Time.time;
GameObject boneInstantiate = Instantiate(shot, shotSpawnLeft.position, shotSpawnLeft.rotation) as GameObject;
BoneScript bone = boneInstantiate.GetComponent<BoneScript>();
//bone.direction.x = Mathf.Cos(270 * Mathf.Deg2Rad);
bone.dir = 1;
anim.SetTrigger("ShootLeft");
Debug.Log(bone.direction.x);
}
if (Input.GetKey(KeyCode.RightArrow) && Time.time > (lastFireTime + burstDelay))
{
lastFireTime = Time.time;
GameObject boneInstantiate = Instantiate(shot, shotSpawnRight.position, shotSpawnRight.rotation) as GameObject;
BoneScript bone = boneInstantiate.GetComponent<BoneScript>();
//bone.direction.x = Mathf.Cos(90 * Mathf.Deg2Rad);
bone.dir = 2;
anim.SetTrigger("ShootRight");
Debug.Log(bone.direction.x);
}
if (Input.GetKey(KeyCode.UpArrow) && Time.time > (lastFireTime + burstDelay))
{
lastFireTime = Time.time;
GameObject boneInstantiate = Instantiate(shot, shotSpawnUp.position, shotSpawnUp.rotation) as GameObject;
BoneScript bone = boneInstantiate.GetComponent<BoneScript>();
//bone.direction.y = Mathf.Cos(0 * Mathf.Deg2Rad);
bone.dir = 3;
anim.SetTrigger("ShootUp");
Debug.Log(bone.direction.y);
}
if (Input.GetKey(KeyCode.DownArrow) && Time.time > (lastFireTime + burstDelay))
{
lastFireTime = Time.time;
GameObject boneInstantiate = Instantiate(shot, shotSpawnDown.position, shotSpawnDown.rotation) as GameObject;
BoneScript bone = boneInstantiate.GetComponent<BoneScript>();
//bone.direction.y = Mathf.Cos(180 * Mathf.Deg2Rad);
bone.dir = 4;
anim.SetTrigger("ShootDown");
Debug.Log(bone.direction.y);
}
}
void OnTriggerEnter2D(Collider2D powerup)
{
boneInstantiate.GetComponent<SpriteRenderer>().sprite = sprArray[0];
}
So I instantiate the object based on the input, then assign it's direction through dir ( will change that to vectors in the near future) and then play an animation. The OnTriggerEnter2D takes the powerup collider as argument, and when I touch it, the sprite of my instantiated object should change but that's not happening for my case. What am I doing wrong? Should the sprite changing script be taking place inside the powerup gameobject?
Answer by Dicklett · Oct 08, 2015 at 04:41 PM
I AM DUMB. I couldn't change sprites cause of the animation running. Now it changes perfectly.
Follow this Question
Related Questions
How to instantiate a new GameObject (sprite) properly? 1 Answer
2D Space Shooter Instantiating bullet question 1 Answer
Change a prefab sprite using Random.range instantiate in a script 3 Answers
How can I make a 2d sprite shoot the direction player is facing? 2 Answers
i want to destroy a projectile after it passes a limit on x axis after instantiation(2d game) 1 Answer