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

[Solved]Can't change variable values on Instantiated object

Discussion in 'Editor & General Support' started by Rafael_Furtado, Aug 22, 2014.

  1. Rafael_Furtado

    Rafael_Furtado

    Joined:
    Aug 22, 2014
    Posts:
    2
    Hi, I'll start with an example

    OtherClassName Go = Instantiate(gameobject, transform.position, Quaternion.identity) as OtherClassName;
    Go.variableOne = 10; //This line isn't read
    Debug.Log("Passed here"); //neither is this one

    Despite variableOne auto completing, or using GetComponent or trying to pass as a paramenter in a function
    I can never change/set a value in one object I just instantiated.
    And it happens in any project no matter how many ways I try to change the code. Even if I copy paste from the manual.
    I've even reinstaled Unity, updated drivers, increased swap memory, disabled mcafee(cause of internal compiiler error);
    This problem got me stuck in my college project and I don't know what else to try, sugestions?
     
  2. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    154
    Code (CSharp):
    1. public class FrameTimer : MonoBehaviour
    2. {
    3.   public Transform prefab;
    4.   public float Variable;
    5.  
    6.   internal void SomeMethod()
    7.   {
    8.     var Go = Instantiate(this.prefab, transform.position, Quaternion.identity) as GameObject;
    9.     var Comp = Go.GetComponent<FrameTimer>();
    10.  
    11.     Comp.Variable = 10; //This line isn't read
    12.     Debug.Log("fooohaari"); //neither is this one
    13.   }
    14.  
    15. *snip*
    16.  
    17.  
    assign a prefab of the object to the Transform prefab field
    also make sure to have watched all tutorials here: http://unity3d.com/learn
     
    naxovr likes this.
  3. Rafael_Furtado

    Rafael_Furtado

    Joined:
    Aug 22, 2014
    Posts:
    2
    Thank you reallly really much it worked,
    I didn't know about var the problem was there.