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
0
Question by JoshMBeyer · Nov 09, 2014 at 02:37 AM · c#arraynull

Access if there are items in an array?

So I have a script, what I want this script to do, have 3 arrays. xFlipObject, yFlipObject, and xyFlipObject. I when this object is clicked there are 2 ways. a single click, and a double click. Single click flips this object horizontally, and double click flips this object vertically. xFlipObject is an object that will flip horiztonally, yFlipObject will flip vertically, and xyFlipObject will be able to flip both vertically or horizontally, depending on single click or double click. These objects mirror the parent object. So xFlip, it will only mirror if the parent object is single clicked. and yFlipObject will only flip if the parent object is doubleclicked. and xyFlipObject will mirror the parent if the parent is single clicked or double clicked. So I have all the programing complete for the flipping and so on and all works just fine. However I want this to be set up where I can have several xFlip, yFlip, and xyFlip. That is where the array comes in. I know I need to be able to detect if there are objects in the slots, and if so, then they may have functionality. If not, then do nothing. How can I do this. Here is the script I have so far. You will notice commented out sections where the old code was. I had a complete code with the mentioned above functionality. Now I just need to set it up for multiple.

 using UnityEngine;
 using System.Collections;
 
 public class TransformTiles : MonoBehaviour
 {
 
     //First slot flips Horizontally.
     //Second slot flips Vertically.
     //Third slot flips bolth Horizontally and Vertically.
     public GameObject[] xFlipObject;
     public GameObject[] yFlipObject;
     public GameObject[] xyFlipObject;
 
     static float clickDelta = 0.35f;  // Max between two click to be considered a double click
 
     private bool click = false;
     private float clickTime;
 
     public int transformer;
 
     void Awake()
     {/*
         if (xFlip != null) { transformer = 1; }
         else if (yFlip != null) { transformer = 2; }
         else if (xyFlip != null) { transformer = 3; }
         else { return; }*/
     }
    
     
     void OnMouseDown()
     {
         if (click && Time.time <= (clickTime + clickDelta))
         {
             //Rotate Vertically.
             click = false;
             if(
         }
         else
         {
             //Rotate Horizontally.
             click = true;
             clickTime = Time.time;
             
         }
 
 
 
 
 
         /*
         //      This "transformer" is a int.
         switch (transformer)
         {
                 //This "1" is an int.  So if transformer = 1 then this code will run.
             case 1:
                 // Rotate Functionality
                 if (click && Time.time <= (clickTime + clickDelta)) 
                 { 
                     //Rotate Vertically.
                     click = false;
                 }
                 else
                 {
                     //Rotate Horizontally.
                     click = true;
                     clickTime = Time.time;
                     FlipHorizontally();
                 }
                 break;
                 //..This "2" is an int. So if transformer = 2 then this code will run.
             case 2:
                 if (click && Time.time <= (clickTime + clickDelta))
                 {
                     click = false;
                     FlipVertically();
                 }
                 else
                 {
                     //Rotate Horizontally.
                     click = true;
                     clickTime = Time.time;
                 }
                 break;
             case 3:
                 if (click && Time.time <= (clickTime + clickDelta))
                 {
                     //Rotate Vertically.
                     click = false;
                     FlipVertically();
                 }
                 else
                 {
                     //Rotate Horizontally.
                     click = true;
                     clickTime = Time.time;
                     FlipHorizontally();
                 }
                 break;
             default: 
          * //do nothing.. 
          *      break;
         }*/
         
     }
 
     void FlipHorizontally()
     {
         //xFlip.GetComponent<NumberMatch>().turnHorizontally();
         
     }
     void FlipVertically()
     {
         //yFlip.GetComponent<NumberMatch>().turnVertically();
     }
     void FlipXandY()
     {
        // xyFlip.GetComponent<NumberMatch>().turnVertically();
        // xyFlip.GetComponent<NumberMatch>().turnHorizontally();
     }
 }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by bubzy · Nov 09, 2014 at 08:10 AM

heres a little click counter script i just wrote :)

 using UnityEngine;
 using System.Collections;
 
 public class mouseClick : MonoBehaviour {
 
     // Use this for initialization
     int clicks =0;
     public float clickTimeout = 0.5f; //change this value to change the time between clicks, lower = faster clicks required.
     float currentTime;
     bool clickBegin = false;
     int maxClicks = 3;
 
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         checkMouseTimer();
     }
 
     void checkMouseTimer()
     {
         if(clickBegin && (Time.time > currentTime || clicks > maxClicks))
         {
             //here you can add checking code for the number of clicks.
             clickBegin = false;
             Debug.Log("clicks : " + clicks);
             clicks = 0;
         }
     }
 
     void OnMouseDown()
     {
         if(!clickBegin)
         {
             clickBegin = true;
             currentTime = Time.time + clickTimeout;
             clicks ++;
         }
 
         else if(clickBegin)
         {
             if(Time.time < currentTime)
             {
                 clicks ++;
                 currentTime = Time.time + clickTimeout;
             }
 
         }
 
 
     }
 
 }
 
Comment
Add comment · Show 3 · 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 JoshMBeyer · Nov 09, 2014 at 11:19 PM 0
Share

This click time I have is short and simple and working properly. I need help with detecting if there are items in the array, and how to access all of them

avatar image bubzy · Nov 10, 2014 at 12:02 AM 0
Share

in what array, you havent even tried to access it in the code you posted.

avatar image JoshMBeyer · Nov 10, 2014 at 01:35 AM 0
Share

yeah because I didn't know how that is what I was trying to ask. Sorry if I wasn't clear. I decided to try foreach and this is the code I have now. But its not working properly. Do you see what I am doin wrong?

 using UnityEngine;
 using System.Collections;
 
 public class TransformTiles : $$anonymous$$onoBehaviour
 {
 
     //First slot flips Horizontally.
     //Second slot flips Vertically.
     //Third slot flips bolth Horizontally and Vertically.
     public GameObject[] xFlipObject;
     public GameObject[] yFlipObject;
     public GameObject[] xyFlipObject;
 
     static float clickDelta = 0.35f;  // $$anonymous$$ax between two click to be considered a double click
 
     private bool click = false;
     private float clickTime;
 
     public int transformer;
 
    
     
     void On$$anonymous$$ouseDown()
     {
         if (click && Time.time <= (clickTime + clickDelta))
         {
             //Rotate Vertically.
             click = false;
             foreach (GameObject obj in yFlipObject)
             {
                 if (obj != null)
                 {
                     obj.GetComponent<Number$$anonymous$$atch>().turnVertically();
                 }
             }
             foreach (GameObject obj in xyFlipObject)
             {
                 if (obj != null)
                 {
                     obj.GetComponent<Number$$anonymous$$atch>().turnVertically();
                 }
             }
         }
         else
         {
             //Rotate Horizontally.
             click = true;
             clickTime = Time.time;
             
             foreach (GameObject obj in xFlipObject)
             {
                 if (obj != null)
                 {
                     obj.GetComponent<Number$$anonymous$$atch>().turnHorizontally();
                 }
             }
 
             
             foreach (GameObject obj in xyFlipObject)
             {
                 if (obj != null)
                 {
                     obj.GetComponent<Number$$anonymous$$atch>().turnHorizontally();
                 }
             }
         }
     }
 }
avatar image
0

Answer by Kiwasi · Nov 10, 2014 at 02:04 AM

Your not looking for this are you? Too much code for me to read all of it.

 if(myArray.Length > 0){
     // Do something amazing
 }

Edit: Just reread your question. Using a generic list and checking the count property would be far more effective then using arrays. Lists are basically arrays that can grow and shrink as required.

Comment
Add comment · Show 1 · 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 JoshMBeyer · Nov 10, 2014 at 03:52 AM 0
Share

i chose array because there will be no need for increasing or decreasing the items in the array. I just need a container to make slots in the inpector

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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reorder array with nulls at the end 1 Answer

Check if Array.GetValue(i) is null 1 Answer

Multiple Cars not working 1 Answer

Array member treated as null even though it is not. 1 Answer

Distribute terrain in zones 3 Answers


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