Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by wertyfal89 · Nov 04, 2016 at 07:13 PM · scripting problemunity4arduino

Splitting script to send signal to arduino

I have a script that sends a signal to the Arduino serial when a cube hits an object. I have multiple cubes, if one cube hits an object it sends "A" to the serial, and when it doesn't hit it should send a lowercase "a", and the next cube sends "B" and "b", the next "C" and "c" and so on.

However i wanted to make a script that i can just attach to each cube, and give them a number by a variable ("fNum") so i can simply add the script to one cube, give the public variable "fNum" a value of 1 and it will send the signal "A" to the serial when it hits something, and then attach it to the next cube and set the variable in the next cube to 2, and it will send the signal "B" and so on, but i have to split up the code into two scripts because the script i made opens the serial and therefore if i attach it to two different cubes it opens two serials and that doesn't work obviously, but i need help with splitting up the code so i have one script attached to each cube, which acts as a sender, and one script that receives signals from all the other cubes and sends the signals out to the serial so it only opens one connection.

The script i have is:

 using UnityEngine;
 using System.Collections;
 using System.IO.Ports;
 
 public class collisionDetection : MonoBehaviour {
 
     public SerialPort serial = new SerialPort ("COM4", 9600);
     public int fNum;
     public bool[] fingers = new bool[]{false, false, false, false };
     public bool on = false;
     public string[] onOff = new string[]{"A","B","C","D"};
 
     void Start () {
         serial.Open ();
     }
 
     void Update(){
         if (fingers[fNum] == true) {
             finger(onOff[(fNum)-1]);
         } else {
             serial.Write (onOff[fNum-1].ToLower);
             on = false;
         }
     }
     
         void OnCollisionEnter (Collision col)
         {
             fingers[fNum] = true;
         }
         void OnCollisionExit(Collision col){
             fingers[fNum] = false;
         }
 
     public void finger(string fNumString){
         if (serial.IsOpen == false)
             serial.Open ();
         if (on == false) {
             serial.Write(fNumString);
             on = true;
         }
     }
 }

Also, the line serial.Write (onOff[fNum-1].ToLower); gives me an error, and i can't figure out how to convert the letter in the array to lowercase.

Any help with this would be greatly appreciated.

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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

error CS0120: An object reference is required to access non-static member 1 Answer

How can I load sprite from previous scene? 1 Answer

Unity 3D: Third person movement and mouse camera control 0 Answers

How to find match in 4 in a Row Game. 1 Answer

Why isn't my player moving down? 0 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