0

I am trying to write this script that allows me to toggle a transform change on/off on a sphere. I think the logic is right but I really can't figure out what I'm missing here. (I am also getting (line 7)error CS0108: 'Scale.transform' hides inherited member 'Component.transform'. Use the new keyword if hiding was intended.)

By the way I'm a total beginner, this might be very simple but this is my first game ever!

Here's the code:

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

public class Scale : MonoBehaviour
{
    public Transform transform;

    Vector3 scale;
    public float objectSize = 3.0f;

    void ScaleOnOff()
    {
        transform = GetComponent<Transform>();

        if (Input.GetKeyDown(KeyCode.S))
        {
            if (scale.x != objectSize)
            {
                scale = transform.localScale;
                scale.x = objectSize;
                transform.localScale = scale;

                Debug.Log("condition1");
            }

            if (scale.x == objectSize)
            {
                scale = transform.localScale;
                scale.x = 1;
                transform.localScale = scale;

                Debug.Log("condition2");
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
            ScaleOnOff();
    }

}

Also, in the console both of the Debug.Log messages appear, so I'm wondering how can both conditions be true at the same time?

Thanks for your help!

  • You first set `scale.x = objectSize;` and right afterwards you check `if (scale.x == objectSize)` - which is obviously `true` since you just set it to that value – UnholySheep Jul 14 '22 at 22:44
  • Remove the `transform = GetComponent();` line. There is already a reference to the transform component inherited from `Component`. – hijinxbassist Jul 14 '22 at 22:56
  • Aah right, so I should have to separate if statements! That makes sense! – newdevnoob Jul 14 '22 at 23:10

1 Answers1

0

There are a few errors, but that's normal if you are a beginner :).

  • There is already a variable called transform. Unity has some shortcuts for getting components: the most used is transform. If you try to delete the "Transform transform" line, in which you rightly declare a variable, you will see that the code will give you no errors. The variable provided by Unity is the same as GetComponent(), so you can delete this line as well, because transform is by default the same as that.
  • In general, if the variables do not change during the game, as in this case with transform, the values ​​must be taken at the beginning of the game, or at least a few times, not continuously. In your code this is not true because the transform variable as mentioned in the previous point already has a value, but for any other variable you assign a value that does not change, it is best to do it in Awake() or Start().
  • Both messages are shown because the code does the following: if you pressed the button, it checks if the value of scale.x is different from "objectSize". (in the first frame scale.x is equal to 0 because you haven't assigned it yet), If it's true (in the first frame it's true), among other things it does, it makes scale.x equal to objectSize. Continuing the code check if scale.x is equal to "objectSsize" (which in the first frame as explained is true), then also execute the second debug.log(). To solve this problem you can try assigning the value of transform.localScale to "scale" in Start(), and instead use two separate if's, an if and an else. On else you can find a lot of documentation online, so I don't want to explain it here.

All together something like e.g.

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

public class Scale : MonoBehaviour
{
    Vector3 scale;
    public float objectSize = 3.0f;

    void Start()
    {
        scale = transform.localScale;
    }

    void ScaleOnOff()
    {
        if (Input.GetKeyDown(KeyCode.S))
        {
            if (scale.x != objectSize)
            {
                scale = transform.localScale;
                scale.x = objectSize;
                transform.localScale = scale;

                Debug.Log("condition1");
            }
            else
            {
                scale = transform.localScale;
                scale.x = 1;
                transform.localScale = scale;

                Debug.Log("condition2");
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
            ScaleOnOff();
    }
}

Excuse me if I was long-winded, but being that you are a beginner, I wanted to explain everything to you well.

Good development!

derHugo
  • 83,094
  • 9
  • 75
  • 115
iFralex
  • 591
  • 1
  • 2
  • 9
  • You're a legend! Can't stress how helpful this is, thank you! – newdevnoob Jul 14 '22 at 23:20
  • It was a pleasure :) – iFralex Jul 15 '22 at 00:23
  • `Unity has some shortcuts for getting components` .. no not really ... but `Component` which `MonoBehaviour` inherits from already implements a property called `transform` ;) – derHugo Jul 15 '22 at 08:34
  • Then note that direct `==` or `!=` checks on `float` are error prone! See [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). You would probably rather want to use e.g. [`Mathf.Approximately`](https://docs.unity3d.com/ScriptReference/Mathf.Approximately.html) – derHugo Jul 15 '22 at 08:38
  • Thanks derHugo, will check some tutorials on Mathf! Cheers :) – newdevnoob Jul 15 '22 at 15:17
  • @derHugo Yes, thanks for the clarification. I did not want to complicate the matter because he is a beginner and right now he does not need to know in detail how it works. Thanks anyway for the addition which can be useful for those who are more experienced. – iFralex Jul 15 '22 at 16:04