Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with a prefab, the public values dont reset when I start the game.

Discussion in 'Scripting' started by Biztor, Nov 11, 2014.

  1. Biztor

    Biztor

    Joined:
    Nov 6, 2014
    Posts:
    11
    I need to access to a script called "Movement", and this script is in a prefab of an asteroid. inside the script I have a public variable, "velocity" for control the velocity of the asteroids.
    ok, then I have an empty object, with a script with the game logic. I need to acces Movement script, for change the variable "velocity



    Code (CSharp):
    1.  public GameObject asteroid;
    2. Movement movement;
    3. void Start()
    4. {
    5. movement = asteroid.GetComponent<Movement>();
    6. }
    7. ...
    8. Void level2()
    9. {
    10. movement.velocity+=5;
    11. }


    in theory, in the game the variables change, but then, when the procces stops, the value should be the original, but isnt... And change the prefab. What I do wrong?

    Thanks for the help.


    I
     
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    You are writing to the prefab directly.... don't you want to instantiate a gameobject from the prefab so it exists in the scene, then write to the values of the instantiated gameObject.
     
  3. Biztor

    Biztor

    Joined:
    Nov 6, 2014
    Posts:
    11
    And how can I do that?
     
  4. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
  5. Biztor

    Biztor

    Joined:
    Nov 6, 2014
    Posts:
    11
    Dont works, with the second method creates an object, not a GameObject, and cant get component "movement" por change it,

    Im ggetting mad, with my prefabs intances (the normal method) in the game I create gameobjects, with the name of the prefab and the " (clone) ", but, later, get component, change values, and modify the prefab instead only the clones in the game..... I dont know...
     
  6. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    I'm not really sure what problem you're experiencing, to be honest.

    If you want to create a version of a prefab in the scene and then change it's values, then you need to Instantiate a copy of it and change the values of that copy.

    Code (CSharp):
    1. public GameObject asteroid;
    2. Movement movement;
    3. void Start()
    4. {
    5. GameObject asteroidInstance = Instantiate(asteroid) as GameObject; // Creates an instance of your prefab in the scene
    6. movement = asteroidInstance.GetComponent<Movement>(); // Gets a reference to this instance's Movement script
    7. }
    8. ...
    9. Void level2()
    10. {
    11. movement.velocity+=5; // Adjusts the velocity value on this instance
    12. }
    That's assuming you only want one instance of the asteroid to be referenced via this script. I suspect you'll have issues with your current code, depending on what you're trying to achieve, though.