x


How can I modify a variable on an instance of a prefab after I created it?

When I manually add the script

var degrees = 20;

function Update() {
    transform.RotateAround (Vector3.zero, Vector3.up, degrees * Time.deltaTime);
}

to the prefab itself I can get the rotation to work. However, when I try and add the rotation inside this prefab creation script I can not get it to work. Moreover, would this have to be inside a update function or how would I get it working all the time?

Essentially what I am trying to do is to slow the rotation as prefab are plotted further away from a given origin. Any help would be greatly appreciated.

var planetPrefab: GameObject;

function Start() { 
    j = 10;
    while (j < 100) { 
        var cloneplanet: GameObject;
        if (planet_class <=100) {
            cloneplanet = Instantiate(planetPrefab, Vector3(sx, sy , sz + j), Quaternion.identity);
            // I want to modify the 'degrees' variable of the script attached to 'cloneplanet' here!
            j = j + 5;
        }   	
    }
}
more ▼

asked Feb 03 '10 at 09:30 PM

Ende gravatar image

Ende
217 5 7 16

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Each time you create an instance of the prefab, set the 'degrees' variable after creating it, like this:

cloneplanet.GetComponent(NameOfFirstScript).degrees = 50/j;
more ▼

answered Feb 03 '10 at 10:48 PM

duck gravatar image

duck ♦♦
41k 92 148 415

Thank you - I was close but not close enough.

Feb 03 '10 at 10:58 PM Ende
(comments are locked)
10|3000 characters needed characters left

If you want to have it permanently attached to the prefab (making it part of the prefab), just add the script to the prefab in the scene, next do menu GameObject -> Apply changes to prefab. You can set the degrees value using:

GetComponent(NameOfScript).degrees = 50 / j;

If you want to add script at runtime you can do:

Make sure the RotateAround script name has no spaces in there and preferably start with an uppercase letter. In the code example below I assume you named it RotateAround.

function Start() { 

    j = 10;

    while (j < 100) { 

        var cloneplanet: GameObject;

        if (planet_class <=100) {
            cloneplanet = Instantiate(planetPrefab, Vector3(sx, sy , sz + j), Quaternion.identity);
            RotateAround rotateAround = cloneplanet.gameObject.AddComponent(RotateAround);
            rotateAround.degrees = 50 / j;

            j = j + 5;
        }           
    }
}
more ▼

answered Feb 03 '10 at 10:50 PM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2086
x1278
x1255
x822
x203

asked: Feb 03 '10 at 09:30 PM

Seen: 2291 times

Last Updated: Feb 04 '10 at 02:01 PM