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 Aydan · Apr 09, 2012 at 07:18 AM · variable

set another scripts variable

Hi I'm just wondering how would I change a scripts variable from another script?

for example:

script1

 var target : GameObject;

script2

 var victim : GameObject;

how do I set the victim game object to the same game object as script1's target?

Comment
Add comment · Show 1
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 Kleptomaniac · Apr 09, 2012 at 07:33 AM 0
Share

You asked this exact same question yesterday: http://answers.unity3d.com/questions/236939/setting-a-variable-to-a-variable-from-another-scri.html ...

2 Replies

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

Answer by GuyTidhar · Apr 09, 2012 at 07:32 AM

script1

 var target : GameObject;
 
 function InitProjectile()
 {
    var newProjectile : Projectile = Instantiate(porjectilePrefab,position,rotation);
    var otherScript : newProjectile.GetComponenet.<script2>();
    target = otherScript.victim;
 }


you should of course update porjectilePrefab, position and rotation to the settings you are using...

Comment
Add comment · Show 15 · 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 Aydan · Apr 09, 2012 at 07:43 AM 0
Share

this works but say im instantiating script2's object and i cant get script1's object so can I do this using script1 to set script2's object?

avatar image GuyTidhar · Apr 09, 2012 at 07:55 AM 0
Share

You can certainly change the scripts and have the Find() function running on script1 to look for script2! I can edit this one if you'd like...

avatar image GuyTidhar · Apr 09, 2012 at 07:58 AM 0
Share

if you are instantiating script2 after script1, why can't you get script1?

avatar image Aydan · Apr 09, 2012 at 08:04 AM 0
Share

because its a tower defence games and there will be multiple turrets which have script1 so I cant find the right one.

avatar image GuyTidhar · Apr 09, 2012 at 08:11 AM 0
Share

Can you have all these GameObject representing the turrets under one GameObject parent? (something you should do in any case...)?

Show more comments
avatar image
0

Answer by DJSwiti · Apr 09, 2012 at 08:58 AM

This is C#. I don't know how to convert a var from JS to something else in C#.. So i converted it in string. I may say wrong

Script 1 :

 using UnityEngine;
 using System.Collections;
 
 public class script_one : MonoBehaviour {
 static string target = GameObject;
 }

And script 2 :

 using UnityEngine;
 using System.Collections;
 
 public class script_two : MonoBehaviour {
 void Update(){
     if(.......){
         script_one.target = ...;
     }
 }
 }

If you can't modify the string target, that is because you must put it in public static string

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

7 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Variable in Inspector 1 Answer

Problems assigning transforms on another script during Start(). 2 Answers

Can't access Depth of Field? 2 Answers

2 small questions about scripting 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