Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-3
Question by DubstepDragon · Aug 20, 2013 at 09:40 PM · c#gui

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joyrider · Aug 20, 2013 at 09:43 PM 1
Share

$$anonymous$$ay I suggest you use arrays for all this with 7 slots ;) Your code would be quite shorter ;)

avatar image DubstepDragon · Aug 21, 2013 at 11:24 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;
             }
         }
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DubstepDragon · Aug 21, 2013 at 06:44 PM 0
Share

Amazing, you are fabulous! Thank you so much, good sir!

avatar image DubstepDragon · Aug 21, 2013 at 08:44 PM 0
Share

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?

avatar image DubstepDragon · Aug 21, 2013 at 09:25 PM 0
Share

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.

avatar image YoungDeveloper · Aug 21, 2013 at 11:39 PM 0
Share

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 ?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is frame, How OnGuI is called every frame? 2 Answers

C# How to Drag and Scale with Mouse Window 0 Answers

Changing GUI.Box opacity 3 Answers

How to dynamically change the text in Unity(Augmented Reality + NYARtoolkit(C#)) ? 0 Answers

[SOLVED] Problem with "foreach". 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges