Freaky
Freaky

Reputation: 3

.GetComponent Function Makes my Variable = 0

StripeNum is on a button and the colour function is set to the same colour button and using Debug.Log it shows stripe1 = 20. But in the second script Debug.Log stripe1.stripe1 = 0. Im just referencing the game object the script is on (the same gameobject is used as the object for the button) Image of The Console Showing Debug.Logs

using UnityEngine;

public class StripeNum : MonoBehaviour
{
    public void black()
    {
        stripe1 = 0;
    }
    public void brown()
    {
        stripe1 = 10;
    }
    public void red()
    {
        stripe1 = 20;
        Debug.Log(stripe1);
    }
    public void orange()
    {
        stripe1 = 30;
    }
    public void yellow()
    {
        stripe1 = 40;
    }
    public void green()
    {
        stripe1 = 50;
    }
    public void blue()
    {
        stripe1 = 60;
    }
    public void purple()
    {
        stripe1 = 70;
    }
    public void grey()
    {
        stripe1 = 80;
    }
    public void white()
    {
        stripe1 = 90;
    }
    public float stripe1;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class FinalCalc : MonoBehaviour
{
    Scene c_scene;
    float TwoStripesAdded;
    float FinalR;
    public Text FinalNum;

        
    void Start(){
        c_scene = SceneManager.GetActiveScene();
        GameObject gameobject =  GameObject.Find("GameObject");
        StripeNum stripe1 = gameobject.GetComponent<StripeNum>();
        Stripe2Num stripe2 = gameobject.GetComponent<Stripe2Num>();
        Stripe3Num stripe3 = gameobject.GetComponent<Stripe3Num>();
        Stripe4Num stripe4 = gameobject.GetComponent<Stripe4Num>();

        if (c_scene.buildIndex == 6){
            
            TwoStripesAdded = stripe1.stripe1 + stripe2.stripe1;
            Debug.Log($"Added {stripe1.stripe1} and {stripe2.stripe1} and got {TwoStripesAdded}");
            FinalR = TwoStripesAdded * stripe3.stripe1;
            
            
            FinalNum.text = FinalR.ToString() + "Ω " + "±" + stripe4.stripe1 + "%";
        }
    }
    
}

Upvotes: 0

Views: 29

Answers (1)

Chuck
Chuck

Reputation: 2102

Why not just name the class Stripe and have multiple instances of it? You can have empty child GameObjects and you can plop each stripe MonoBehaviour on the empty child. Then you can name the GameObject Stripe1, Stripe2, etc.

In Start() you can find those GameObjects, get the component attached to each, and cache those references so you don't have to keep re-getting them.

Aaaand actually in looking at your code I was assuming you were running this in Update, but you're running the check in Start. I don't know when or how you're setting colors, but if you're doing it in Play mode it's already too late, because this script won't check the colors.

You can't activate the methods until play mode (which is why I was asking you how you set it), so the only way to get a reading is if you set the value in Editor mode, but again that's not going to call a method.

Maybe what you're doing is setting the value in Editor mode that corresponds to a color?

Whatever the case, I think your data is not initialized when you check it, which is only once, before the first frame is drawn. If you're getting other debug messages then I need to know how you're getting. The Sum should be printed before anyone gets to set a value in Play mode.

Also a thought - you're checking the scene for some reason? If you set values and change scenes, all your objects are destroyed. The resistor calculator in one scene is a different instance than the one in scene 6 and will not have any values set on any stripes.

Upvotes: 0

Related Questions