Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by IBDelta · Oct 25, 2010 at 08:10 PM · instantiatescriptingbasicsclone

Clones with individual scripts

I'm still having difficulty with instantiated prefabs. I've made a small star map to see if I can change a variable on an individual prefab but I seem to have hit a brick wall. The following scripts show no errors but the debug.log fails to print anything. The first script is the clone manager which is attached to an empty game object in the hierarchy, the second is attached to the prefab.

StarGenScript:

var BasicStarPrefab:GameObject; // The prefab I am cloning var CloneScript:BasicStarScript; // The script that should be attached to each clone var StarGenScript:StarGenScript; // The clone managers script

//The basic attributes of each star static var XCurrentStar; static var YCurrentStar; static var ZCurrentStar; static var CurrentStarType; static var CurrentStarNumber;

// Call this function once at the start function Awake () { StarGenerate(); }

function StarGenerate (){ for (var CountNum : int = 0;CountNum < 1000; CountNum++) { // Randomly determine the position of each star XCurrentStar = Random.Range(0.0, 10.0); YCurrentStar = Random.Range(0.0, 10.0); ZCurrentStar = Random.Range(0.0, 10.0);

 //  Set the identification number and type of star
 CurrentStarNumber = CountNum;
 CurrentStarType = Random.Range(1, 2);

 //  Make a vector of the random coordiantes and instantiate a star
 var position = Vector3(XCurrentStar,YCurrentStar,ZCurrentStar);
 var NewBasicStarPrefab = Instantiate(BasicStarPrefab, position, Quaternion.identity);
 NewBasicStarPrefab.name = "BasicStar";  //  Rename the clone to BasicStar

 //  Access the clones script and send some variables to it
 CloneScript = NewBasicStarPrefab.GetComponent(BasicStarScript);
 CloneScript.StarNumber = StarGenScript.CurrentStarNumber;
 CloneScript.StarType = StarGenScript.CurrentStarType;   
 }

}

BasicStarScript:

static var StarType; static var StarNumber;

function Update () { Debug.Log(StarNumber); Debug.Log(StarType);

 if(StarType==1){
     renderer.material.color = Color.black;
 }
 if(StarType==2){
         renderer.material.color = Color.white;
 }

}

If someone could fix these scripts and post a working pair I would be eternally grateful

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by lhk · Oct 26, 2010 at 03:44 PM

Static variables are shared by all scripts of one type. As you have attached the BasicStartScript to the prefab each object has an "individual" copy of this script but there are no "individual" variables in it. You could try to send a message to your new star object to set the values. Try these scripts (in c#) StarGenScript

using UnityEngine; using System.Collections;

public class StarGen : MonoBehaviour { float XCurrentStar; float YCurrentStar; float ZCurrentStar; float CurrentStarType; float CurrentStarNumber; Transform BasicStarPrefab;

// Use this for initialization void Start () { StarGenerate(); }

// Update is called once per frame void Update () {

}

void StarGenerate() { for (int CountNum = 0;CountNum < 1000; CountNum++) { // Randomly determine the position of each star XCurrentStar = Random.Range(0.0f, 10.0f); YCurrentStar = Random.Range(0.0f, 10.0f); ZCurrentStar = Random.Range(0.0f, 10.0f);

     //  Set the identification number and type of star
     CurrentStarNumber = CountNum;
     CurrentStarType = Random.Range(1, 2);

     //  Make a vector of the random coordiantes and instantiate a star
     Vector3 position =new Vector3(XCurrentStar,YCurrentStar,ZCurrentStar);
     GameObject NewBasicStarPrefab =(GameObject) Instantiate(BasicStarPrefab, position, Quaternion.identity);
     NewBasicStarPrefab.name = "BasicStar";  //  Rename the clone to BasicStar
     //  Access the clones script and send some variables to it
     NewBasicStarPrefab.SendMessage("SetStarNumber",CurrentStarNumber);
     NewBasicStarPrefab.SendMessage("SetStarType",CurrentStarType);
 }

}

}

BasicStarScript

using UnityEngine;

using System.Collections;

public class BasicStarScript : MonoBehaviour { int StarType; int StarNumber;

void Update () {

 // I don't think you want to write something to the
 // Debug output everytime Update is called
 //Debug.Log(StarNumber);    
 //Debug.Log(StarType);

 if(StarType==1)
 {
     renderer.material.color = Color.black;
 }
 if(StarType==2)
 {
     renderer.material.color = Color.white;
     }

}

// Use this for initialization void Start () {

}

// Update is called once per frame void SetStarNumber(int number) { StarNumber=number; } void SetStarType(int type) { StarType=type; }

}

I hope this works and I could help you.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Instantiate script then edit its variables 2 Answers

How to instantiate from prefab, not from instance? 3 Answers

Sometimes one sometimes two clone is formed. I just want to form a clone 1 Answer

Pass a copy of a GameObject as variable to another script? 1 Answer

need help in the instantiation script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges