- Home /
Problems with instantiation...
I have a basic game that has seven buttons down the bottom of the screen. When one is pressed, it sets the defaultSpell variable to that spell, ranging from spell1 to spell7. However, it does not seem to work as intended. i am getting no errors, however it is not working as I want it to. It is not instantiating any others when I press the button. My two scripts are here:
LocalInstantiation
using UnityEngine; using System.Collections;
public class LocalInstantiation : MonoBehaviour {
public GameObject defaultSpell; public GameObject spell1; public GameObject spell2; public GameObject spell3; public GameObject spell4; public GameObject spell5; public GameObject spell6; public GameObject spell7; public static bool spell1active; public static bool spell2active; public static bool spell3active; public static bool spell4active; public static bool spell5active; public static bool spell6active; public static bool spell7active; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if(defaultSpell == null) { Debug.Log(""); } if(Input.GetMouseButtonDown(0)) { Ray ray; RaycastHit hit; ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 100.0f)) { if (hit.collider.name == "Plane") Instantiate(defaultSpell, hit.point, Quaternion.identity); } } if(spell1active == true) { defaultSpell = spell1; spell2active = false; spell3active = false; spell4active = false; spell5active = false; spell6active = false; spell7active = false; } if(spell2active == true) { defaultSpell = spell2; spell1active = false; spell3active = false; spell4active = false; spell5active = false; spell6active = false; spell7active = false; } if(spell3active == true) { defaultSpell = spell3; spell1active = false; spell2active = false; spell4active = false; spell5active = false; spell6active = false; spell7active = false; } if(spell4active == true) { defaultSpell = spell4; spell1active = false; spell2active = false; spell3active = false; spell5active = false; spell6active = false; spell7active = false; } if(spell5active == true) { defaultSpell = spell5; spell1active = false; spell2active = false; spell3active = false; spell4active = false; spell6active = false; spell7active = false; } if(spell6active == true) { defaultSpell = spell6; spell1active = false; spell2active = false; spell3active = false; spell4active = false; spell5active = false; spell7active = false; } if(spell7active == true) { defaultSpell = spell7; spell1active = false; spell2active = false; spell3active = false; spell4active = false; spell5active = false; spell6active = false; } } }
MainGUI
using UnityEngine; using System.Collections;
public class MainGUI : MonoBehaviour {
public Texture2D icon; void OnGUI () { if(GUI.Button(new Rect(100, 500, 75, 75), new GUIContent("", icon))) { LocalInstantiation.spell1active = true; } if(GUI.Button(new Rect(200, 500, 75, 75), new GUIContent("", icon))) { LocalInstantiation.spell2active = true; } if(GUI.Button(new Rect(300, 500, 75, 75), new GUIContent("", icon))) { LocalInstantiation.spell3active = true; } if(GUI.Button(new Rect(400, 500, 75, 75), new GUIContent("", icon))) { LocalInstantiation.spell4active = true; } if(GUI.Button(new Rect(500, 500, 75, 75), new GUIContent("", icon))) { LocalInstantiation.spell5active = true; } if(GUI.Button(new Rect(600, 500, 75, 75), new GUIContent("", icon))) { LocalInstantiation.spell6active = true; } if(GUI.Button(new Rect(700, 500, 75, 75), new GUIContent("", icon))) { LocalInstantiation.spell7active = true; } } }
Thanks in advance!
$$anonymous$$ay I suggest you use arrays for all this with 7 slots ;) Your code would be quite shorter ;)
Please do not downvote my posts, if you have any objections, please refer to the FAQ and highlight where I have gone wrong. Can you see it? No, of course, so do not do it again.
Answer by Joyrider · Aug 20, 2013 at 09:56 PM
My guess would be, as it is now, you get stuck on the first spell. I'd use an int instead of your bools...
So with arrays and an int instead of your bools, it would look something like this:
using UnityEngine;
using System.Collections;
public class LocalInstantiation : MonoBehaviour {
public GameObject defaultSpell;
public GameObject[] spell = new GameObject[7];
public static int activeSpell;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(defaultSpell == null)
{
Debug.Log("No default spell");
}
if(Input.GetMouseButtonDown(0))
{
Ray ray;
RaycastHit hit;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100.0f)) {
if (hit.collider.name == "Plane")
Instantiate(defaultSpell, hit.point, Quaternion.identity);
}
}
}
}
using UnityEngine;
using System.Collections;
public class MainGUI : MonoBehaviour {
public Texture2D[] icon;
void OnGUI ()
{
for(int j=0; j<7;j++)
{
if(GUI.Button(new Rect((j+1)*100, 500, 75, 75), new GUIContent("", icon[j])))
{
LocalInstantiation.activeSpell = j;
}
}
}
}
Amazing, you are fabulous! Thank you so much, good sir!
O$$anonymous$$G wait, you did something wrong, what I want is the Default Spell to be empty at first! THat's the whole point, then a button sets the default spell to that spell. Can you help me with this?
flaviusxvii, I was wasted the hope that an answer was brought about here, and an email in my inbox too. All this was for you complaining? As I clearly stated, I did not need anyone to write my code for me, I only needed assistance, but Joyrider was generous enough to do the whole thing for me. And, clearly enough, I asked of the same matter right now, with your protests against so. Please go here and read why UnityAnswers was created. I hope you will understand in due course that it is a place for beginners and through to achieve higher knowledge of how Unity3D works and to ask assistance from those who are experienced.
You are posting 150+ lines of code here. I don't see a solution "assisting" you without scripting the thing for you, because explaining would be just too long . And as soon as you get negative feedback, you are hiding behind "beginner" mask, even if your question isn't even that beginnish. $$anonymous$$inda pathetic you know ?