Search Unity

need help making script

Discussion in 'Scripting' started by ariusdb222, Mar 3, 2015.

  1. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    So i decided to take a new approach to making biomes on the infinite terrain...
    Basically 3 small scripts that is much easier and faster to make
    Script 1 finds all terrains in scene, then for each terrain, loads script 2.
    Script 2 uses Random to choose a random biome. Then loads biome based on the random choice.

    So every terrain made has its own biome, and the infinite terrains also get biomes..... just need a way to update the new terrains with biomes...

    NO i am not a programmer, just need to get a few things wotking...

    My first question, how do i find all the terrains in the scene?
    I tried something like this but doesn't work:mad:

    Code (CSharp):
    1. terrain = gameObject.AddComponent<randomBiome> ();
    terrains need to get the randomBiome script...

    How can i make this work?
    Any help is appreciated....
     
    Last edited: Mar 4, 2015
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I don't want to discourage you, but it seems like you may be aiming too far above your current skill level. Have you considered following along with some of the tutorials or example projects to help get your bearings?

    If you're sure you're ready to proceed, one way to get all the objects of a certain category in a scene would be to assign all of those objects a tag and then use GameObject.FindGameObjectsWithTag().
     
  3. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    tried this but doesn't work..

    Code (CSharp):
    1. var terrains = Terrain.activeTerrains;
    2. terrains.AddComponent<randomBiome>();
    and tried with tags..
    i already made the randomBiome script and it works great.. but dont know how to do this, please help..:confused:
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    "It doesn't work" is rarely a helpful explanation. There are many ways that something can "not work" (doesn't compile, crashes, executes but gives no output, causes your computer to burst into flames...).

    Computer programming is all about precision. It's like bargaining with an evil genie: you can get your wishes granted, but only if you word them exactly right, because the computer is going to give you exactly what you asked for, not what you meant.

    In this case, you are covering up critical details by using the "var" keyword instead of the actual type of the variable. According to this, Terrain.activeTerrains has a return type of Terrain[]. That means that your "terrains" variable is an array. Arrays don't have an AddComponent method, so you can't do terrains.AddComponent.

    You need a foreach loop. (Well, some kind of loop; but foreach is probably the best option in this particular case.)
     
  5. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    Ok thanks for the reply. basically it didn't work, gave a few errors.. so is another way to add my script to the terrains?

    Also there is 1 terrain in the scene, it has the infinite terrains script attached to it, and make terrains around the player as the player moves it deletes terrains and adds new one's....
    So basically, i can't attach the randomBiomes script to the terrains... since they only generate at runtime.

    was planning to use foreach loop to add script to terrains.;)
    any help would be appreciated..
     
  6. MagicZelda

    MagicZelda

    Joined:
    May 1, 2013
    Posts:
    90
    best to look for video tutorials on basic c# programming in unity and then try a few easier things first. Then after a while come back to this.
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    /facepalm