Question by
artosking · Dec 11, 2015 at 01:12 PM ·
c#instantiateprefabs
Instantiation of my GameObjects spell (from other script)
Hi guys! This is my first post here:)
I'm making an rpg game, where you play with a Mage.
So - I have 2 scripts for this. "Shot" and "SpellChanger"
The Shot script (the variables are in Polish, so sry;) ):
public class Strzal : MonoBehaviour {
public float czekaj = 10f; // "wait"
public float odliczanieDoStrzalu = 0f; // "countingToShot"
public Transform ShotSpawn;
public float obrazenia = 50.0f; //"damage"
void Start () {
}
void Update () {
if (odliczanieDoStrzalu <czekaj) {
odliczanieDoStrzalu += Time.deltaTime;
}
if(Input.GetMouseButton(0) && odliczanieDoStrzalu >= czekaj){
odliczanieDoStrzalu = 0;
SpellChanger projectile = GetComponent<SpellChanger>(); // ***< this is after some "delete", so it looks like it looks (and i know this is completly wrong***
Instantiate(projectile, ShotSpawn.position, transform.rotation);
}
}
void OnTriggerExit(Collider other)
{
Destroy (GameObject.FindWithTag("Spell"));
}
}
And here's a SpellChanger
using UnityEngine;
using System.Collections;
public class SpellChanger : MonoBehaviour {
public GameObject[] Spell;
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Q))
{
SwitchSpellPlease();
WhichSpell ();
}
}
void WhichSpell()
{
if (Spell [0].activeSelf) {
Debug.Log ("Czar 1");
}
if (Spell [1].activeSelf) {
Debug.Log ("Czar 2");
}
if (Spell [2].activeSelf) {
Debug.Log ("Czar 3");
}
}
void SwitchSpellPlease()
{
if (Spell [0].activeSelf) {
Spell[0].SetActive (false);
Spell[1].SetActive (true);
Spell[2].SetActive (false);
}
else if (Spell [1].activeSelf) {
Spell[0].SetActive (false);
Spell[1].SetActive (false);
Spell[2].SetActive (true);
}
else if (Spell [2].activeSelf) {
Spell[0].SetActive (true);
Spell[1].SetActive (false);
Spell[2].SetActive (false);
}
}
}
The Debug.Log in SpellChanger works, so in Console, after pressing Q key, i see that spells are changing. But after fireing them, my Mage character is Instantiated not the Spell prefab.
Help! :)
Comment