#(Solved) Issue modifying certain properties of instantiated objects

1 messages · Page 1 of 1 (latest)

winter gulch
#

Hello, I am attempting to make an asteroids game for a school course I am taking. In the script for the object I have that is responsible for spawning asteroids, I instantiate a new asteroid with randomly generated position and velocity (which is decided before the code snippet I will paste below) and then attempt to enable the sprite renderer and collider for the newly instantiated object because these properties are initially disabled in the start method of asteroids to avoid the original asteroids i am instantiating from showing up to the user. I am using this code to accomplish this:
In my game object

            a.GetComponent<Rigidbody2D>().velocity = vel;
            a.gameObject.GetComponent<SpriteRenderer>().enabled = true;
            a.gameObject.GetComponent<CircleCollider2D>().enabled = true;```
In my asteroid within the start method
``` gameObject.GetComponent<Renderer>().enabled = false;
        gameObject.GetComponent<CircleCollider2D>().enabled = false;```

Interestingly, the velocity and position of the newly instantiated asteroids work fine (as observed by looking at scene view) but the render and collider remain disabled. Any advice? Thank you
#

I also tried using setActive(false) and setActive(true) but had similar issues