Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

It isn't static but acts like one

Discussion in 'Scripting' started by Michaelsito1995, Oct 22, 2021.

  1. Michaelsito1995

    Michaelsito1995

    Joined:
    Mar 17, 2013
    Posts:
    8
    Hi.

    I have a problem with this code.


    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class Pez : MonoBehaviour
    5. {
    6.     public GameObject PrefabPez;
    7.     public List<Transform> Posiciones = new List<Transform>();
    8.     public List<Material> MaterialPeces = new List<Material>();
    9.     public int numPeces = 30;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         for (int i = 0; i < numPeces; i++)
    15.         {
    16.             int rangoVariable = Random.Range(0, Posiciones.Count - 1);
    17.             Vector3 pos = new Vector3(Posiciones[rangoVariable].position.x, Posiciones[rangoVariable].position.y, Posiciones[rangoVariable].position.z);
    18.  
    19.             var fish = Instantiate(PrefabPez, pos, Quaternion.Euler(0, 0, 0));
    20.  
    21.             //Here's the issue
    22.             fish.transform = gameObject.transform;
    23.             var material = fish.GetComponentInChildren<MeshRenderer>();
    24.             int rangoMateriales = Random.Range(0, MaterialPeces.Count - 1);
    25.             material.material = MaterialPeces[rangoMateriales];
    26.         }
    27.     }
    28. }
    I don't know in which part of my code I made this Prefab or this GameObject a static, cause, when I try to parent it to another GameObject, this message shows saying that is just to read.

    Anyone have any idea of what is happening.

    I tried making another script from scratch and writing all over again, but the issue is still happening.
     
  2. What's the exact error in the console, could you please share with us?

    FYI: This is not right. The integer version of the
    Random.Range
    is exclusive on the max side, specifically for these use cases. This should be
    Random.Range(0, MaterialPeces.Count)
    . Also see the documentation here: https://docs.unity3d.com/ScriptReference/Random.Range.html
     
  3. Michaelsito1995

    Michaelsito1995

    Joined:
    Mar 17, 2013
    Posts:
    8
    @Lurking-Ninja thanks for your reply.

    I'll follow your recommendations for the Random.Range.

    About the static issue, this is the complete message: CS0200: Property or indexer 'GameObject.transform' cannot be assigned to -- it is read only
     
  4. And what did you try to do? That's always readonly since transforms are mandatory and cannot be removed or changed (only its properties). Maybe you're missing a
    .position
    there?
     
    Last edited by a moderator: Oct 22, 2021
  5. Michaelsito1995

    Michaelsito1995

    Joined:
    Mar 17, 2013
    Posts:
    8
    I'm trying to parent all the created prefabs to the gameobject with the script.
     
  6. Delete this line:
    fish.transform = gameObject.transform;

    Replace this line:
    var fish = Instantiate(PrefabPez, pos, Quaternion.Euler(0, 0, 0));
    with this line:
    var fish = Instantiate(PrefabPez, pos, Quaternion.identity, gameObject);
    .
    I think this should work.
     
    Bunny83 and Michaelsito1995 like this.
  7. Michaelsito1995

    Michaelsito1995

    Joined:
    Mar 17, 2013
    Posts:
    8
    Nice, thanks a lot, It work just perfect.
     
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,920
    Or, in case you use a ancient Unity version where the Instantiate method didn't have that additional parent argument, you would have to do

    Code (CSharp):
    1. fish.transform.parent = gameObject.transform;
     
  9. Michaelsito1995

    Michaelsito1995

    Joined:
    Mar 17, 2013
    Posts:
    8
    Thanks a lot, all of you. You solved my problem. Now the program works just fine.