How do you edit a shape during runtime?

Avatar
  • updated
  • Answered

Hi, I'm trying to edit the radius of a regular polygon during runtime (so it can become bigger and smaller as an animation) but the usual code for accessing a component variable of a game object in Unity isn't working because the script "Regular Polygon" isn't recognized, even though MS Visual Studio and the script for Regular Polygon clearly shows that "Radius" is a float variable of the Regular Polygon script.

What I have is "Circle AoE" being passed as a "Game Object" into a script, and then I try to edit that using the standard "Get Component" code but it refuses to link to the Regular Polygon script and gives the error message: 'GameObject' does not contain a definition for 'RegularPolygon' and no accessible extension method 'RegularPolygon' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?).

So how are shapes edited by scripts during runtime? Can this be done on shapes that are made in the Unity WYSIWYG editor by scripts, or do I need to make the object by script first before I can edit it during runtime (this would cause a whole new set of problems if I tried it).

Reporting a bug? please specify Unity version:
Reporting a bug? please specify Shapes version:
Reporting a bug? please specify Render Pipeline:
Built-in render pipeline
Pinned replies
Avatar
Freya Holmér creator
  • Answer
  • Answered

if you got the error

'GameObject' does not contain a definition for 'RegularPolygon'
and no accessible extension method 'RegularPolygon' accepting
a first argument of type 'GameObject' could be found
(are you missing a using directive or an assembly reference?)

it means somewhere you typed myGameObject.RegularPolygon, which isn't valid code since there's no field in game objects that automatically finds custom components

like I mentioned on twitter, GetComponent<RegularPolygon>() is the correct way to get those components, and if you get that error or it doesn't work, then it's somewhere in your code that there's either a syntax error or you have some weird caching issues where Unity isn't compiling the latest scripts for you

Avatar
Johannes Deml

For me accessing the radius is as straight forward as it gets. Here is a simple example script:

using Shapes;
using UnityEngine;

public class RegularPolygonPulseAnimation : MonoBehaviour
{
    private RegularPolygon polygon;
    private float maxRadius;

    private void Awake()
    {
        polygon = GetComponent<RegularPolygon>();
        maxRadius = polygon.Radius;
    }

    private void Update()
    {
        polygon.Radius = (Mathf.Sin(Time.time) + 1f) * 0.5f * maxRadius;
    }
}

Example of how it is attached:

Avatar
❄️ Ollie ❄️
Quote from Johannes Deml

For me accessing the radius is as straight forward as it gets. Here is a simple example script:

using Shapes;
using UnityEngine;

public class RegularPolygonPulseAnimation : MonoBehaviour
{
    private RegularPolygon polygon;
    private float maxRadius;

    private void Awake()
    {
        polygon = GetComponent<RegularPolygon>();
        maxRadius = polygon.Radius;
    }

    private void Update()
    {
        polygon.Radius = (Mathf.Sin(Time.time) + 1f) * 0.5f * maxRadius;
    }
}

Example of how it is attached:

Thanks for the reply. Fairly sure I've tried code like this, though I'll copy your example word for word and try it tomorrow, and it didn't work because "RegularPolygon" simply isn't recognised.

Not even Fraya could explain why when I asked her, but yeah, I'll try your code and see what happens...

EDIT: I legit don't know why but your code did work when I tried it, and it's smoother than the work around I made using Transform->Scale for what I was trying to do. Anyway, it worked and that's all that counts, cheers. 

Avatar
Freya Holmér creator
  • Answer
  • Answered

if you got the error

'GameObject' does not contain a definition for 'RegularPolygon'
and no accessible extension method 'RegularPolygon' accepting
a first argument of type 'GameObject' could be found
(are you missing a using directive or an assembly reference?)

it means somewhere you typed myGameObject.RegularPolygon, which isn't valid code since there's no field in game objects that automatically finds custom components

like I mentioned on twitter, GetComponent<RegularPolygon>() is the correct way to get those components, and if you get that error or it doesn't work, then it's somewhere in your code that there's either a syntax error or you have some weird caching issues where Unity isn't compiling the latest scripts for you