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 Struders · Jul 27, 2013 at 05:48 PM · inspectorscriptingbasics

Getting values from inspector into scripts

Super beginner question how do I get this value into my script? (c#)

alt text

untitled-3.jpg (46.2 kB)
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

2 Replies

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

Answer by tw1st3d · Jul 27, 2013 at 06:05 PM

Click on the script that is on your object,(Targeting.cs) and you'll see the variable being created inside of that. Next, create a new script, let's say TargetingModifier.cs

 using UnityEngine;
 using System.Collections;
 using System;
 
 class TargetingModifier : MonoBehavior
 {
 
     // I don't have Targeting.cs, so it's either
     // An asset, or your own code, so we'll say 
     // its class name is Target
     
     public Target theTarget = new Target();
     // Create a new variable instantiating
     // A new instance of class "Target"
     
     // Next, call a variable from it.
     public Transform getTarget = theTarget.SelectedTarget;
     
     // Now, let's do something with it.
     void Update()
     {
         if(!getTarget == null)
         {
             // If the target is actually set,
             getTarget.Destroy();
             // Destroy it
         }else{
             // If it's NOT set,
             getTarget = GameObject.Find("TheTarget");
             // Set it as a target
         }
     }
 }
Comment
Add comment · Show 2 · 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 Struders · Jul 27, 2013 at 06:19 PM 0
Share

Absolutely spot on, Thanks!

avatar image tw1st3d · Jul 27, 2013 at 06:20 PM 0
Share

You're welcome, glad I was able to help!

avatar image
0

Answer by Owen-Reynolds · Jul 27, 2013 at 06:24 PM

If you already have that script made, drag anything from the "scene objects" panel (named Hierachy) into that slot. You have to select the thing with the script (like your picture) and then drag the other thing (without clicking on it.) If it works, it will highlight and "None(Transform)" will be replacead. You double-check by clicking that slot. The real item should turn yellow.

In the script the variable SelectedTarget will magically be aimed at that thing you dragged.

It is legal to drag a prefab into the slot (from Project,) but don't. Your target would be something not in the game yet, so wouldn't make sense (it will just do weird stuff.)

Of course, that only works it the object will always have that one thing for a target. If it can switch, the script has to do the work. Then there's no point dragging.

Comment
Add comment · Show 3 · 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 tw1st3d · Jul 27, 2013 at 06:25 PM 0
Share

I don't think he's asking how to set an object into the variable, but how to access the variable itself through code.

avatar image Struders · Jul 27, 2013 at 06:30 PM 0
Share

Yeah the over all goal is to lock on to a target and then fire missiles at the target.

Just didn't know how to pass the value from the targeting script to the missile guidance script.

avatar image Owen-Reynolds · Jul 28, 2013 at 04:41 AM 0
Share

The giant picture of the Inspector, and "Getting from Inspector" in the title, threw me off.

While the program runs, the Inspector is just a view of some variables, and doesn't do anything. So, in code, you never have to do anything special to set or read an Inspector variable. Just write a regular program and set/read everything the usual way.

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

17 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

Related Questions

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

Values lost? 1 Answer

Not every Class in the API is derived from the "Object"-Class, so what are they? 2 Answers

Script to teleport player (Beginner here) 0 Answers

Find Object with variable, not a name. 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