Jump to content

C# Unity Help.

werto165

I'm trying to access a color variable from another script and use that in a script to assign a sphere which is attached to my finger to see what color it will come out when pinchdrawing(Leap motion). 

coloring = GameObject.Find("Pinch Draw").GetComponent<PinchDraw>().ColorMem;  

this above line of code works however I was wondering why this wouldn't work: 

 

using UnityEngine;
using System.Collections;
using Leap.Unity.DetectionExamples;

public class pin : MonoBehaviour {
    public Renderer rend;
    private PinchDraw _PinchDraw;
    Color coloring; 
    // Use this for initialization
    void Start () {
        
    }
	void Awake()
    {
        


    }
    // Update is called once per frame
    void Update()
    {   
        rend = GetComponent<Renderer>();
        coloring = GameObject.Find("Pinch Draw").GetComponent<PinchDraw>().ColorMem; 
        
        PinchDraw _PinchDraw = GetComponent<PinchDraw>(); //These two lines don't work
        coloring = _PinchDraw.ColorMem;					// above 
        if (_PinchDraw != null)
        {
            Debug.Log("It worked!"); 
            rend.material.color = _PinchDraw.ColorMem;

        }

        rend.material.color = coloring; 




    }
    
}

Other script in question: 

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI; 

namespace Leap.Unity.DetectionExamples {

  public class PinchDraw : MonoBehaviour {
    public float colorRed;
    public float colorGreen;
    public float colorBlue;
    public Color ColorMem;
    public Button rButton;

 

Do I have to assign that script to a gameobject? 

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to comment
Share on other sites

Link to post
Share on other sites

That script is a MonoBehaviour. it only works when attached to a gameObject. You can get rid of the base class of monobehavoir and make it a normal class, but you still need to instantiate it somewhere.

 

You're also defining this twice

PinchDraw _PinchDraw

 In your update you are saying "look on this gameobject for the attached component of type "pinchdraw"" which doesn't exit. 

 

I advise against "getcomponent" inside update or any other loop. it's really performance heavy. do it outside in your initialisation and only call it when necessary.

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

The leap script looks like something external though so it's best to assign it to a game object, since that's how it's meant to be used. But still, do the "getcomponent" in init, or even make the instance public and drag it into the script in the inspector, then you don't have to do the work in the code.

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, squirrl said:

That script is a MonoBehaviour. it only works when attached to a gameObject. You can get rid of the base class of monobehavoir and make it a normal class, but you still need to instantiate it somewhere.

 

You're also defining this twice


PinchDraw _PinchDraw

 In your update you are saying "look on this gameobject for the attached component of type "pinchdraw"" which doesn't exit. 

 

I advise against "getcomponent" inside update or any other loop. it's really performance heavy. do it outside in your initialisation and only call it when necessary.

So the 

private PinchDraw _PinchDraw;

I would get rid of? 

 

I think I could easily set it up as another (don't know the name for it) but say: void GetColor(){

code for finding game object and assigning it to that colour variable I have. 

} that code is only run when I press the button on my unity UI.

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to comment
Share on other sites

Link to post
Share on other sites

public PinchDraw _PinchDraw; and then drag the pinch draw into the inspector. Makes sense that works. 

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to comment
Share on other sites

Link to post
Share on other sites

@squirrl 

 

using UnityEngine;
using System.Collections;
using Leap.Unity.DetectionExamples;

public class pin : MonoBehaviour {
    public Renderer rend;
    public PinchDraw _PinchDraw;
    
    Color coloring; 
    // Use this for initialization
    void Start () {
        rend = GetComponent<Renderer>();
    }
	void Awake()
    {
        


    }
    // Update is called once per frame
    void Update()
    {   
        
        
        rend.material.color = coloring; 




    }
   public void GetColor() {
        coloring = _PinchDraw.ColorMem;
    }
    
}

Anyway of improving this? 
          
          

It seems like the best I can do, whenever I press that button that says "Press" at the moment it runs the GetColor() 

Demo.png

Button.png

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to comment
Share on other sites

Link to post
Share on other sites

Why not set the colour in GetColour, only when it changes rather than setting it every frame?

public void GetColor() {
    rend.material.color = coloring;
}

 

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, squirrl said:

Why not set the colour in GetColour, only when it changes rather than setting it every frame?


public void GetColor() {
    rend.material.color = coloring;
}

 

Yeah that seems like a good shout. Just a question, those 3 sliders control the RGB elements of the color to do a real time view of the color without affecting performance is there a way to say poll the update to something like 0.1 - 0.2 secs rather than 0.011secs as it currently is(assuming 90FPS). I guess I could tie the script to the sliders too i.e when depressed it will change color. 

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to comment
Share on other sites

Link to post
Share on other sites

53 minutes ago, werto165 said:

Yeah that seems like a good shout. Just a question, those 3 sliders control the RGB elements of the color to do a real time view of the color without affecting performance is there a way to say poll the update to something like 0.1 - 0.2 secs rather than 0.011secs as it currently is(assuming 90FPS). I guess I could tie the script to the sliders too i.e when depressed it will change color. 

 
 

Yeah, you can poll. create an integer as a private counter, and another one which defines the poll time. In Update add Time.deltaTime to your counter and then check if it's greater than or equal to the poll time. If it is, then reset the counter to 0, and do your action.

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×