Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

how to connect the circuit by using raycast click

Discussion in 'Scripting' started by nurulhidayah, Nov 15, 2018.

  1. nurulhidayah

    nurulhidayah

    Joined:
    Oct 22, 2018
    Posts:
    7
    I need help in scripting. i want to develop AR logic gates circuit. there will have two input, Gates which is AND, OR, and others and bulb as the output..then when the output is 1 the bulb will turn red, and black if the output is 0. i did the script but it doesn't work.
    i used raycast script for toggle switch which is user can click on it. when user click on, the value is 1, while off will be 0 value.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    What doesn't work?
     
  3. nurulhidayah

    nurulhidayah

    Joined:
    Oct 22, 2018
    Posts:
    7
    I mean, the output.... For example, AND gate formula is (if input_A = 1 && input_B = 1), the output will 1. else, output 0.
    In output script, if value =1, turn on light. Else, turn off light.
    But, the bulb (light) is still same, not change
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Maybe show us this "script" you speak of.
     
  5. nurulhidayah

    nurulhidayah

    Joined:
    Oct 22, 2018
    Posts:
    7
    THIS IS MY TOGGLE SCRIPT

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Vuforia;
    using System.IO;

    public class click_A : CircuitBlock
    {

    public GameObject off_A, on_A;

    // Use this for initialization
    void Start ()
    {
    off_A = GameObject.Find("off_A");
    on_A = GameObject.Find("on_A");
    }

    // Update is called once per frame
    void Update ()
    {


    if(Input.GetMouseButtonDown(0))
    {
    Value = (Value + 1) % 2;

    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;

    if(Physics.Raycast(ray, out hit))
    {
    if (Value == 1)
    {
    if (hit.collider.tag == "off_A")
    {

    off_A.SetActive(false);
    on_A.SetActive(true);
    Value = 1;
    }
    }

    else
    {
    if (hit.collider.tag == "on_A")
    {

    off_A.SetActive(true);
    on_A.SetActive(false);
    Value = 0;
    }
    }
    }
    }
    }
    }
    ------------------------------------------------------------------------------------------------------------------------------------------------------------
    THIS IS MY AND_GATE SCRIPT

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Vuforia;
    using System.IO;

    public class AND_Gate : CircuitBlock
    {
    void UpdateBlock ()
    {
    //no inputs, so the logical result is 0
    if (InputBlocks == null)
    {
    Value = 0;
    return;
    }

    //if any input is 0, then the logical result is 0
    //also accept null inputs as 0
    foreach(CircuitBlock input in InputBlocks)
    {
    if (input == null || input.Value == 0)
    {
    Value = 0;
    return;
    }

    else
    {
    Value = 1;
    return;
    }
    }
    //else, the logical result is 1
    //Value = 1;
    return;
    }
    }
    ---------------------------------------------------------------------------------------------------------------------------------------------------------

    THIS IS MY SCRIPT FOR BULB as FOR LIGHT(OUTPUT)

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Vuforia;
    using System.IO;

    public class light : CircuitBlock {

    public GameObject lightoff, lighton;

    // Use this for initialization
    void StartBlock()
    {
    // This light will initially be turned off
    //lighton.GetComponent<Renderer>().enabled = false;
    //lightoff.GetComponent<Renderer>().enabled = true;

    lighton.SetActive(false);
    lightoff.SetActive(true);
    Value = 0;
    }

    void UpdateBlock()
    {
    //if any input is null or 0 then turn the light off
    /*foreach (CircuitBlock block in InputBlocks)
    {
    if (InputBlocks == null || block.Value == 0)
    {
    TurnOff();
    return;
    }
    }*/
    //if the input list is empty or null then the light is turned off
    if (InputBlocks == null || InputBlocks.Count == 0)
    {
    TurnOff();
    return;
    }

    else
    {

    //else, turn the light on
    TurnOn();
    return;
    }
    }

    private void TurnOff()
    {
    lighton.GetComponent<Renderer>().enabled = false;
    lightoff.GetComponent<Renderer>().enabled = true;
    Value = 0;
    }

    private void TurnOn()
    {
    lighton.GetComponent<Renderer>().enabled = true;
    lightoff.GetComponent<Renderer>().enabled = false;
    Value = 1;
    }


    }
     
  6. nurulhidayah

    nurulhidayah

    Joined:
    Oct 22, 2018
    Posts:
    7
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    upload_2018-11-16_10-51-56.png
     

    Attached Files:

  8. nurulhidayah

    nurulhidayah

    Joined:
    Oct 22, 2018
    Posts:
    7
    i'm using visual studio 2017. can it be use in it?
     
  9. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    You really should try first before asking.
    Most of us will be using now VS, unless anyone is still on older version for any reason, where VS is optional.
     
  10. nurulhidayah

    nurulhidayah

    Joined:
    Oct 22, 2018
    Posts:
    7
    already try and got error
     
  11. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    What error you have?
    What does it says?
     
  12. nurulhidayah

    nurulhidayah

    Joined:
    Oct 22, 2018
    Posts:
    7
    Assets/Vuforia/Script/Circuit.cs(1,5): error CS1525: Unexpected symbol `=', expecting `(', `,', `.', `:', `::', `]', or `<'
    Assets/Vuforia/Script/Circuit.cs(57,1): error CS1525: Unexpected symbol `/', expecting `event', `identifier', or `return'
    Assets/Vuforia/Script/Circuit.cs(59,1): error CS0589: Internal compiler error during parsingSystem.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
     
  13. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    That is error in VS isn't it?
    This is not what Code Tag instruction suggests.

    Read and try again. If fail, you need describe steps, of what you are doing.
     
  14. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Heyyo!
    #MonoDevelop