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
0
Question by potter3366 · Jul 28, 2013 at 02:48 PM · rigidbodyprefabdestination

SOLVED - How to access variable from another script for Instantiate prefab

  1. script is attached to empty GO-create characters in circle positions (ringspawn1.js)

  2. script is attachet to prefab character (move1.js)

How to assign endPosition Vector3 from ringspawn1.js script to each individual character in move1.js script ? Using static variable index from ringspawn1.js in second script doesn't work properly.

     #pragma strict
     
     static var index : int;
     
     var count : int = 7;
     var radius : float = 25.0;
     var angleOffset : float = 0.0;
     var prefab : Transform;
     
     var agents : ArrayList;
     var targets : ArrayList;
     private var pos : Vector3;
     private var rot : Quaternion;
     
     function Start () {
     
         agents = new ArrayList();
         targets = new ArrayList();
         
         for (var i=0;i<count;i++) {
             index = i;
             var angle : float = i;
             angle /= count;
             rot = Quaternion.Euler(0, angle * 360.0 + angleOffset, 0);
             pos = rot * Vector3.forward * - 1 * radius;
             var newAgent = Instantiate(prefab, pos + transform.position, rot);
             agents.Add(newAgent.gameObject);
             
             var target = new GameObject("Target" + index).transform;
             target.position = rot * Vector3.forward * 20 + newAgent.transform.position + Vector3(0,2,0); 
             targets.Add(target.transform);
             
         }
         
     }
     
     
     #pragma strict
     
     private var desiredVelocity : Vector3;
     private var lastSqrMag : float;
     private var Speed1 : float = 2.2;
     private var startPosition : Vector3; 
     private var endPosition : Vector3;
     
     function Start () {    
         startPosition = transform.position;
         endPosition = GameObject.Find("Target" + ringspawn1.index).transform.position;
     
         var directionalVector : Vector3 = (endPosition - transform.position).normalized * Speed1;
         lastSqrMag = Mathf.Infinity;
         desiredVelocity = directionalVector;    
     }
     
     function Update()
     {
         var sqrMag : float = (endPosition - transform.position).sqrMagnitude;
         if ( sqrMag > lastSqrMag )
         {
             desiredVelocity = Vector3.zero;
         }
         lastSqrMag = sqrMag;
     }
      
     function FixedUpdate()
     {
         rigidbody.velocity = desiredVelocity;
     }
Comment
Add comment · Show 2
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
avatar image nsxdavid · Jul 28, 2013 at 05:43 PM 0
Share

$$anonymous$$gest you consult the official tutorials on scripting: http://unity3d.com/learn/tutorials/modules/beginner/scripting

avatar image Loius · Jul 28, 2013 at 11:18 PM 0
Share

this question has been asked many times

every time it is 'how to access a variable on another script'

p.s.: it's "how do i"

you need to have an object of move1 and then you can use it however you want.

var ob : move1 = Instantiate(stuff)

ob.thing = 7;

2 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

Answer by potter3366 · Jul 29, 2013 at 12:36 PM

 // ringspawn1.js
 #pragma strict
 
 static var objects : GameObject[];
 static var objectsTransform : Transform[];
  
 var count : int = 7;
 var radius : float = 40.0;
 var angleOffset : float = 0.0;
 var prefab : Transform;
 @HideInInspector
 var index : int;
 var targets : ArrayList;
 
 private var pos : Vector3;
 private var rot : Quaternion;
  
 function Start () {
 
     objects = new GameObject[count];
     objectsTransform = new Transform[count];
     
     targets = new ArrayList();
  
     for (var i=0;i<count;i++) {
         index = i;
         var angle : float = i;
         angle /= count;
         rot = Quaternion.Euler(0, angle * 360.0 + angleOffset, 0);
         pos = rot * Vector3.forward * - 1 * radius;
         
         var thisObject = Instantiate(prefab, pos + transform.position, rot);
         thisObject.name = "beggar" + i;
  
         var target = new GameObject("Meta" + i).transform;
         target.position = rot * Vector3.forward * 35 + thisObject.transform.position + Vector3(0,2,0);
         targets.Add(target.transform);
         
         puniPolje(i, thisObject.gameObject, target.transform);
  
     }
 }
 
 function puniPolje (ii : int, g : GameObject, t : Transform)
 {
     objects[ii] = g;
     objectsTransform[ii] = t;
 }


 // move1.js
 #pragma strict
 
 private var Speed1 : float = 2.2;
 private var startPosition : Vector3; 
 private var endPosition : Vector3;
 
 private var startTime: float;
 private var journeyLength: float;
 private var myHit : RaycastHit;
 private var offsetAlign : float = 0.1;
 
 function Start () 
 {    
     startTime = Time.time;
 
     startPosition = transform.position;
     
     for (var i=0;i<ringspawn1.objectsTransform.Length;i++) {
         if(name == "beggar" + i) endPosition = ringspawn1.objectsTransform[i].position;
     }
     
     journeyLength = Vector3.Distance(startPosition, endPosition);    
 }
 
 function Update()
 {
     var distCovered = (Time.time - startTime) * Speed1;
     var fracJourney = distCovered / journeyLength;
     transform.position = Vector3.Lerp(startPosition, endPosition, fracJourney);
     
     if(Physics.Raycast (transform.position, Vector3.down, myHit))
     {
          transform.position.y = myHit.point.y + offsetAlign;
     }
 }
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
avatar image
2

Answer by abeldantas · Jul 28, 2013 at 11:28 PM

I think I'm not understanding your question, but I'll bite:

In move.js declare a Gameobject to hold your empty Go that has ringspawn1.js attached:

 var ringspawnGO : Gameobject;

Don't forget to assign the gameObject to this variable in the editor. Then instead of :

 GameObject.Find("Target" + ringspawn1.index).transform.position; 

You can do:

 lastOneIndex = ringspawnGO.GetComponent("ringspawn1").count;
 lastGO = GameObject.Find("Target" + lastOneIndex);
 
 endPosition = lastGo.transform.position;


This approach has a problem, you need to link your ringspawnGO to the script in your prefab. If you use your prefab in another scene, you will need to change the link manually. To access gameobject more dynamically you can use GameObject.Find (works by name) or GameObject.FindGameObjectsWithTag.

Additionally you shouldn't name your scripts with numbers, names should describe what you are implementing.

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

changing the speed of an enemy on key press? 2 Answers

spawning rigidbodies next to each other 0 Answers

Trigger between 2 kinematic bodies not firing? -1 Answers

Ignore Collision Between Model and Player 2 Answers

How to assign an Prefab to MainBody using 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